Skip to content

Issue with AddFile on Request #1714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iamsubingyawali opened this issue Jan 19, 2022 · 7 comments
Closed

Issue with AddFile on Request #1714

iamsubingyawali opened this issue Jan 19, 2022 · 7 comments

Comments

@iamsubingyawali
Copy link

I am using Restsharp to send a POST request to my REST API running Laravel. Specifically, what I am trying to do is to create a new user from my WPF app which sends some user details and profile image as post params. I tried the same thing with Postman and it is working fine but it didn't work as expected while using Restsharp. I am getting PHP Warning: File Upload Mime headers garbled in Unknown on line 0 on my Laravel server.

Code I am using:

RestRequest restRequest = new RestRequest("user", Method.Post); 
restRequest.AlwaysMultipartFormData = true;
restRequest.AddHeader("Content-Type", "multipart/form-data");

// Adding other post params
// post_data is a dictionary: Dictionary<string, object> post_data;
post_data.Add("firstname", FirstNameField.Text.Trim());
// and so on
restRequest.AddJsonBody(post_data);

// Adding files
// profile_image is link to the file: E:\MyFiles\Pictures\Screenshots\Screenshot (7).png
restRequest.AddFile("photo", profile_image);
var response_json = await client.ExecuteAsync<JObject>(restRequest);

Environment

  • OS: Windows 11
  • .NET5 WPF
  • Version 107.1.1
  • Server: Laravel v8.58.0 (PHP v8.0.13)
@alexeyzimarev
Copy link
Member

How does the request look like in Postman?

@alexeyzimarev alexeyzimarev removed the bug label Jan 19, 2022
@alexeyzimarev
Copy link
Member

Please use something like https://requestbin.com to compare the request posted with Postman, and with RestSharp. Expand the headers section, and use raw content view when making screenshots.

@iamsubingyawali
Copy link
Author

iamsubingyawali commented Jan 19, 2022

Headers from Postman

'Accept' => 'application/json',
'Authorization' => 'Bearer 245|jbjikfnOxkck73NmwRll8SEwcSbnjVnylTGZEv5m',
'User-Agent' => 'PostmanRuntime/7.29.0',
'Postman-Token' => '058ccabe-398c-4d85-8766-b65084cdae04',
'Host' => '127.0.0.1:8000',
'Accept-Encoding' => 'gzip, deflate, br',
'Connection' => 'keep-alive',
'Content-Type' => 'multipart/form-data; boundary=--------------------------182914604267830295666128',
'Content-Length' => '1015565',

Headers from Restsharp

'Accept' => 'application/json',
'Authorization' => 'Bearer 244|KhV6cMu1dWCgXDKFs1oSFHs5fxQdK7FYWXdnURkr',
'User-Agent' => 'RestSharp/107.1.1.0',
'Host' => '127.0.0.1:8000',
'Accept-Encoding' => 'gzip, deflate, br',
'Content-Type' => 'multipart/form-data; boundary="6671f68a-aa4e-46dc-9af1-7f884a5d08b0"',
'Content-Length' => '1014828',

Sorry to say that I misssed something earlier. The request from Restsharp is not sending params other than one from AddFile. Is this something that is impossible to do with Restsharp?

I tried only sending the image and it worked. But with other content, I am getting the same error.

@alexeyzimarev
Copy link
Member

I don't think you are posting JSON with Postman, I think you post the form data. Please just take a screenshot from requestbin.com of the raw request coming from Postman.

@iamsubingyawali
Copy link
Author

iamsubingyawali commented Jan 19, 2022

Yes, I am using form-data to send the data from Postman as we can't attach file using other formats in Postman. I extracted those requests from the server itself. I tried using AddBody instead of AddJsonBody in Restsharp but it too didn't work.

@alexeyzimarev
Copy link
Member

You need to make a choice, whether you are posting form data, or you are posting a JSON payload.

Posting form parameters with RestSharp is done by using AddParameter(name, value), as it is described in the docs.

@iamsubingyawali
Copy link
Author

I think AddParameter fulfils my need. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants