C# send multipart form-data request with GET verb - c#

I know it's unorthodox, but I'm trying (C# - .NET Framework 4.8) to send a multipart form-data request with GET verb.
I need to send a file and some parameters, but the end-point receiving the call blocks POST/PUT requests without a valid (for them) JSON body, and I have no control over it.
I managed to do that with Postman, and this is the curl command:
curl --location --request GET 'https://myrestapi.com' \
--header 'MyPersonalHeader: mydata' \
--header 'Content-Type: application/json' \
--form '=#"/C:/mypath/myfile.zip"' \
--form 'MyParameter1="myvalue1"' \
--form 'MyParameter2="myvalue2"' \
The end-point accepts this call.
How can I make it in C#?

Related

Generate cURL request from WebAPI HTTP Request c#

Trying to generate a cURL string from an incoming request in my c# web api. Not sure how to proceed. I could of course take all parts and create it myself but I was wondering if there are some easier ways of doing it?
For example a request:
curl --location --request POST 'http://localhost:54521/v2/Token'
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=usr' \
--data-urlencode 'password=password' \
--data-urlencode 'grant_type=password'
Then generating it again on my API - as a string, that I could use with cURL. Suggestions?

Get 400 Bad Request when calling MailChimp API 3.0 using CURL request in Windows cmd

I'm working with MailChimp API v3.0 and I need to create a new Segment, based on the MailChimp API documentation: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/
It's not working in my C# API Wrapper and I´m using CURL (suggested in the documentation) for testing purposes, specifically CURL for Windows: https://curl.haxx.se/ ...here is my command:
curl --request POST --url https://usXX.api.mailchimp.com/3.0/lists/my1i5t1d/segments --header "Authorization: apikey myap1k3y-usXX" --header 'Content-Type: application/json' --data '{"name":"myDynamicSegment"}' --include
and the response message is:
HTTP/1.1 400 Bad Request
Server: nginx
Content-Type: application/problem+json; charset=utf-8
Content-Length: 203
X-Request-Id: 1d001b85-0286-4339-ab37-3cb49c6f28f8
Link: <https://us13.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json>; rel="describedBy"
Date: Thu, 14 Jul 2016 22:06:08 GMT
Connection: close
Set-Cookie: _AVESTA_ENVIRONMENT=prod; path=/
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"JSON Parse Error","status":400,"detail":"We encountered an unspecified JSON parsing error.","instance":""}
Using Curl in windows cmd, the response only said:
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"JSON Parse Error","status":400,"detail":"We encountered an unspecified JSON parsing error.","instance":""}
But alternatively I used Git Bash( included by default when you install Git for Windows https://git-for-windows.github.io/ ) and the response was different and very useful:
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"Must provide either an \"options\" field or a \"static_segment\" field","instance":""}
Based on this message I added the "static_segment" parameter in JSON and everything works great when I adjusted the command option --data:
curl --request POST --url https://usXX.api.mailchimp.com/3.0/lists/my1i5t1d/segments --header "Authorization: apikey myap1k3y-usXX" --header 'Content-Type: application/json' --data '{"name":"myDynamicSegment", "static_segment" : ["test1#domain.com", "test1#domain.com"]}' --include
Comments about the problem/solution:
I strongly recommend to use another command line (not Curl for Windows), in my case I used Git Bash for Windows and showed me the real issue.
MailChimp API documentation http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/ only have "name" as required parameter and it's not real: "options" or "static_segment" field are required.
Problem solved!

Watson dialog cURL conversation post request not passing form data

When making a post cURL request as below to try and continue a created conversation watson instead returns a new conversation.
curl -u "USERNAME":"PASSWORD" -X POST --form conversation_id=CONVOID --form client_id=CLIENTID --form input="What type of toppings do you have?" "https://gateway.watsonplatform.net/dialog/api/v1/dialogs/DIALOGID/conversation"
If i use the below cURL it works fine.
curl -u "USERNAME":"PASSWORD" --data "conversation_id=CONVOID&client_id=CLIENTID&input=What type of toppings do you have?" https://gateway.watsonplatform.net/dialog/api/v1/dialogs/DIALOGID/conversation
My issue being that now when trying to write a c# wrapper i'm running in to the same issue that POST requests fail to transmit their form data correctly.
What's going on ?
I either need a c# MVC equivalent to the "--data" formatting. ( currently using HttpClient.PostAsync) or to figure out what is exactly wrong with using post requests to continue conversations.
As far as i can tell i am replicating the post request in c# correctly so i don't think there are two issues. ( just one post request issue, not a cURL issue then a C# implementation issue.)
For what it's worth i have left the commands in the format i submitted them, only replacing sensitive values with BLOCKCAPITALS. If it looks like i've missed a quotation mark or curly bracket , it's because i have and may be the cause of the issue.
The service expects an application/x-www-form-urlencoded POST request
To do that in curl you need to use the -d parameter:
curl -u "USERNAME":"PASSWORD" -X POST
-d conversation_id=CONVOID
-d client_id=CLIENTID
-d input="What type of toppings do you have?"
"https://gateway.watsonplatform.net/dialog/api/v1/dialogs/DIALOGID/conversation"
-d, --data
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.
Curl documentation

C# code for curl operation

curl for Twilio with out helper library
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/[sid]/Messages.xml' \
--data-urlencode 'To=+919400xxxxxx' \
--data-urlencode 'From=+1484925xxxx' \
--data-urlencode 'Body=[body]' \
-u [sid]:[AuthToken]
I wanted to Do it using C# code. ( I don't have much knowledge in C# )
I just checked these
Making a cURL call in C#
cURL with user authentication in C#
But gives invalid request.
You can start by using Nuget to download the Twilio helpers, search for Twilio.
Once you have that package installed, you can use it like this.
var client = new TwilioRestClient("accountsid", "authtoken");
var result = client.SendSmsMessage("from", "to", "body");

multipart/form-data webclient c#

I need to send a request of this type:
curl \
-H 'Authorization: OAuth AJOBibdmecfKxOpEeRWbdmVVCGujGRU3JKA0jyQM' \
-F 'lecture\[media\]=#video.avi;type=video/mpeg' \
-F 'lecture\[name\]=My lecture' \
-F 'lecture\[type\]=Media' \
http://www.redu.com.br/api/subjects/117/lectures -vv
I want to use the WebClient class in C# to make a multipart POST request for sending JSON + file to be loaded, but do not know how I can do this, can anyone help me?

Categories

Resources