Reputation: 1
I´m trying to consume a Rest service that receives an image with a key. I am using RestSharp v.106.10.1 in Visual Studio 2017. I have the following code, which when executing returns a message with the server response that says the package has arrived without the image. Can you help me on what am I doing wrong?.
This is my code
var client = new RestClient("http://localhost:3030/api/upload-image");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddFile("images", "/C:/Users/Desktop/AppTestWM/ServidorFTP/450_1000.jpg");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
The way to send from postman is this. the header contentType: application/x-www-form-urlencoded and the body in form-data added a key “image” and the image file.
This is header format
This is body format
MORE IINFORMATION
This is postman console good answer
POST http://localhost:3000/imagenSoliTag/upload-image
• ▶Network
▶Request Headers
User-Agent: PostmanRuntime/7.24.1
Accept: /
Cache-Control: no-cache
Postman-Token: 44761995-f969-417d-9216-c98d2ee38b35
Host: localhost:3000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=------------------------- -505415277809498326289672
Content-Length: 20233
• ▶Request Body
▶image: {…}
_events: {}
_eventsCount: 3
▶_readableState: {…}
autoClose: true
bytesRead: 20022
closed: true
domain: null
fd: null
flags: "r"
mode: 438
path: "C:\Users\Desktop\AppTestWM\ServidorFTP\450_1000.jpg"
readable: false
▶Response Headers
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 71
ETag: W/"47-DTIxeI6y0HsQCMVrZcYol72rDDs"
Date: Sun, 19 Apr 2020 23:42:08 GMT
Connection: keep-alive
• ▶Response Body
response: "¡Imagen guardada con éxito!"
imagePath: "450_1000.jpg"
This is postman console bad answer when make a query without image
POST http://localhost:3000/imagenSoliTag/upload-image
• ▶Network
▶Request Headers
User-Agent: PostmanRuntime/7.24.1
Accept: /
Cache-Control: no-cache
Postman-Token: 9903417e-e3f0-467e-a185-046a1043ba45
Host: localhost:3000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 0
• Request Body
▶Response Headers
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 50
ETag: W/"32-Z/sIWz99etyVziu3PXRpXUZOG6c"
Date: Sun, 19 Apr 2020 23:44:07 GMT
Connection: keep-alive
• ▶Response Body
statusCode: 400
message: "¡Elija una imagen!"
This is Visual studio debug in Request
and in Response debug is this
Upvotes: 0
Views: 3385
Reputation: 7187
The answer is in your question.
See the Content-Type sent by postman:
And this is the request you are posting with RestSharp (gathered using Fiddler 4)
POST /api/upload-image HTTP/1.1
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/106.0.0.0
Content-Type: application/x-www-form-urlencoded; boundary=-----------------------------28947758029299
Host: oguzozgul.com.tr
Content-Length: 19006
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
It is actually trying to send the form using boundaries, but you are forcing it to send the Content-Type header as application/x-www-form-urlencoded
Set the header Content-Type as multipart/form-data, or (works with the latest version), let RestSharp set the Content-Type automatically by not forcing it to use a specific (and incorrect) one: (The statement setting the Content-Type header is removed:)
var client = new RestClient("http://localhost:3030/api/upload-image");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddFile("images", "/C:/Users/Desktop/AppTestWM/ServidorFTP/450_1000.jpg");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Upvotes: 0