Uploading multipart/form-data on Android using OKHttp - okhttp

I was trying to upload Multipart/form-data on Android using Okhttp, but it does not work.
The request header is
POST /api/texttospeech/v3.0-beta1//datasets/upload HTTP/1.1
Host: koreacentral.customvoice.api.speech.microsoft.com
Connection: keep-alive
Content-Length: 201455
Accept: application/json, text/plain, /
Correlation-Id: 290c5850-f621-11ea-ac4a-9dac0e19cc7b
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36 Edg/85.0.564.51
Ocp-Apim-Subscription-Key: 38fae10960fc477ba614c6dfe64613ba
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryZAaHsREIpzJ9sBHS
Origin: https://speech.microsoft.com
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Accept-Encoding: gzip, deflate, br
Accept-Language: ko,en;q=0.9,en-US;q=0.8
and the multipart/form-data is
------WebKitFormBoundaryZAaHsREIpzJ9sBHS
Content-Disposition: form-data; name="projectId"
1a685f5d-400b-4801-8adb-9d359cad27cb
------WebKitFormBoundaryZAaHsREIpzJ9sBHS
Content-Disposition: form-data; name="name"
16151
------WebKitFormBoundaryZAaHsREIpzJ9sBHS
Content-Disposition: form-data; name="description"
51561
------WebKitFormBoundaryZAaHsREIpzJ9sBHS
Content-Disposition: form-data; name="dataImportKind"
CustomVoice
------WebKitFormBoundaryZAaHsREIpzJ9sBHS
Content-Disposition: form-data; name="locale"
en-US
------WebKitFormBoundaryZAaHsREIpzJ9sBHS
Content-Disposition: form-data; name="properties"
{"Gender":"Male","IsMixLingual":"false","PortalAPIVersion":"3"}
------WebKitFormBoundaryZAaHsREIpzJ9sBHS
Content-Disposition: form-data; name="audiodata"; filename="1.zip"
Content-Type: application/x-zip-compressed
------WebKitFormBoundaryZAaHsREIpzJ9sBHS
Content-Disposition: form-data; name="transcriptions"; filename="transcript.txt"
Content-Type: text/plain
------WebKitFormBoundaryZAaHsREIpzJ9sBHS--
The code I created is something like this.
String boundary = "----WebKitFormBoundary" + result; //result is the random digits
String Url = String.format("https://%s.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0-beta1//datasets/upload", region);
String audioPath = getFilesDir().getAbsolutePath()+"/audios.zip";
String scriptPath = getFilesDir().getAbsolutePath()+"/script.txt";
System.out.println("properties to String"+ properties.toString());
File audioFile = new File(audioPath);
File scriptFile = new File(scriptPath);
//Create request body (params or json)
System.out.println("audiopath is " + audioPath);
System.out.println("scriptpath is "+ scriptPath);
MultipartBody formBody = new MultipartBody.Builder(boundary)
.setType(MultipartBody.FORM)
.addFormDataPart("projectId", projectId)
.addFormDataPart("name", defaultVal)
.addFormDataPart("description", defaultVal)
.addFormDataPart("dataImportKind", "CustomVoice")
.addFormDataPart("locale", "en-US")
.addFormDataPart("properties", properties.toString())
.addFormDataPart("audiodata", "audios.zip", RequestBody.create(MediaType.parse("application/x-zip-compressed"), audioFile))
.addFormDataPart("transcriptions", "script.txt", RequestBody.create(MediaType.parse("text/plain"), scriptFile))
.build();
System.out.println(formBody);
Request request = new Request.Builder()
.header("Ocp-Apim-Subscription-Key", subKey)
.addHeader("Ocp-Apim-Subscription-Key", subKey)
.addHeader("Content-Type", formBody.contentType().toString())
.post(formBody)
.url(Url)
.build();
System.out.println("Data upload request : "+ request);
System.out.println("Data upload request : "+ request.body());
System.out.println("Data upload request : "+ request.body().toString());
//Response response = client.newCall(request).execute();
//System.out.println("data upload response "+response.body().string());
//response.close();
Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure(#NotNull Call call, #NotNull IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(#NotNull Call call, #NotNull Response response) throws IOException {
System.out.println("Response "+response);
}
});
return null;
}
The error is
Response{protocol=http/1.1, code=500, message=Internal Server Error, url=https://koreacentral.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0-beta1//datasets/upload}
The Internal Server Error seems to be wrong, since it works if I send the request through Python, using
from requests_toolbelt.multipart.encoder import MultipartEncoder

Code 500 means the server receives your request but fail to complete it.As the server performs well while using python according to your description, I guess that you got something wrong in your HTTP-Header or multipart/form-data.But it cannot be find out yet by the only info.I suggest that you should compare the data you send through okhttp and python to find out the difference.

Related

issue posting multipart/form-data via html5 FormData to c# Api

My issue is that my c# api controller is kicking back theFormData Post request when I check for multi-part data here: IsMimeMultipartContent(), which then throws the message back to the UI:
415 (Unsupported Media Type)
[HttpPost]
[Route("MediaUpload")]
public async Task<HttpResponseMessage> MediaUpload([FromUri]string sessionId, [FromUri]string patientId)
{
if (!Request.Content.IsMimeMultipartContent())
{ // *** ALWAYS THROWS ERROR ***
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
//access form data
var provider = await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider());
NameValueCollection formData = provider.FormData;
//... additional code omitted
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("UploadPath", fullPath);
response.Headers.Add("Access-Control-Allow-Origin", "*");
return response;
}
My front end payload looks like this:
REQUEST HEADERS
POST /api/import/MediaUpload?sessionID=c83f9589-742e-40e3-8cf5-7ffff141c3d7&patientId=5981 HTTP/1.1
Host: localhost:56703
Connection: keep-alive
Content-Length: 6545
Accept: application/json, text/plain, */*
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
Content-Type: multipart/form-data
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
REQUEST PAYLOAD
------WebKitFormBoundaryVrKSHPqdT9c6JkKv
Content-Disposition: form-data; name="enctype"
multipart/form-data
------WebKitFormBoundaryVrKSHPqdT9c6JkKv
Content-Disposition: form-data; name="MediaInfo"
[{"PatientID":5981,"PatientLastName":"Hobo","PatientFirstName":"John","DeviceID":"123","InstanceID":89,"PatientDOB":"1/3/1970","FileName":"image2.jpg","FileSize":5880,"ExamDate":"5/30/2018","PatientID":66665981,"SessionID":"sessionID=9999141c3d7"}]
------WebKitFormBoundaryVrKSHPqdT9c6JkKv
Content-Disposition: form-data; name="files[]"; filename="image2.jpg"
Content-Type: image/jpeg
ÿØÿàJFIFÿÛC
------WebKitFormBoundaryVrKSHPqdT9c6JkKv--
My front end is sending in FormData, generated in the following TypeScript method, then calling the importService further below:
save() {
let params = { patientID: ''};
let exDate = new Date(this.defaultDate).toLocaleDateString();
// Populate MediaInfo object
let minfo = this.SetMediaArray();
params.pID = minfo[0].PID;
const formData = new FormData();
formData.set("enctype", "multipart/form-data" ); // doesn't make a difference..
formData.append("MedInfo", JSON.stringify(minfo));
for(var i=0; i<this.importImages.length; i++){
// append images to the files[] array, then send formData object to impService
formData.append("files[]", this.dataURItoBlob(this.importImages[i].TnUrl), this.importImages[i].name);
}
let endpoint = this.selectedObj.Url;
this.importService.saveImportObjects(formData, params, endpoint).subscribe(
data=> {
console.log(data);
},
err => {
console.log(err);
}
);
}
So basically I would like to know exactly what http option I'm missing for this to work.
thank you.
I removed the multipart/boundary header attribute, and let the browser take care of it for you. The upload worked.

WebApi HttpPost not working with parameters

I have a Web Api project with this route config
var config = GlobalConfiguration.Configuration;
config.Routes.MapHttpRoute(
"DefaultApiRoute",
"api/v1/{controller}/{action}");
I have two post method
[HttpPost]
public IHttpActionResult RetrieveTestNoParam()
{
return new JsonResult<string>("This is a test", new JsonSerializerSettings(), Encoding.UTF8, this);
}
[HttpPost]
public IHttpActionResult RetrieveTest(string input)
{
return new JsonResult<string>(input, new JsonSerializerSettings(), Encoding.UTF8, this);
}
The first one is working but the second one gives this respone
{
"Message": "No HTTP resource was found that matches the request URI 'http://test.dev.local/api/v1/NewsApi/RetrieveTest'.",
"MessageDetail": "No action was found on the controller 'NewsApiController' that matches the request."
}
This is what I see in fiddler
POST http://test.dev.local/api/v1/NewsApi/RetrieveTest HTTP/1.1
Host: test.dev.local
Connection: keep-alive
Content-Length: 261
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
Content-Type: application/javascript
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
Postman-Token: c2ba631b-4bfd-9379-bf40-0ca1a6a8a3bc
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,nl;q=0.6
Cookie: ASP.NET_SessionId=4kgfwd2u3pwusfdvdsrkt2vt; SC_ANALYTICS_GLOBAL_COOKIE=ddd9185a3e8940c9823e81b7d38cdf20|False; sc_expview=0
------WebKitFormBoundaryxdYhzJ7A8WnI8qaa
Content-Disposition: form-data; name="input"
Dit is de input
------WebKitFormBoundaryxdYhzJ7A8WnI8qaa
Content-Disposition: form-data; name="czczcsdcsdf"
sdfsfsdfsdff
------WebKitFormBoundaryxdYhzJ7A8WnI8qaa--
Any ideas?
Other questions I have about Web Api:
Can a controller contain multiple get or post methods?
Do I need the actionname attribute and what does this attribute do?
Regards
Danny

Using CommonsMultipartResolver to parse an HTTP request with embedded multipart content

I am using Spring Integration to receive an HTTP request which has multipart content nested inside a multipart request as shown below. When using CommonsMultipartResolver as the resolver the LinkedMultiValueMap passed to the receiving service activator contains a map of all the parts but the imbedded multipart is an UploadedMultipartFile which contains the entire content. In the sample boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL is the boundary of the embedded multipart.
How can I get the embedded multipart parsed into a LinkedMultiValueMap so that I can get the version and payload? Is it possible for the CommonsMultipartResolver to do this or is there another way to parse it?
Http Request:
POST http://xxxxx:8087/edi846Inbound HTTP/1.1
Content-Type: multipart/form-data;boundary=DM1-Sb0636VXPQNO3412Ygegb9suqr
.......
Content-Length: 2787
--DM1-Sb0636VXPQNO3412Ygegb9suqr
Content-Disposition: form-data; name="from"
Content-Type: text/plain;charset=ISO-8859-1 Content-Length: 7
gisb-acknowledgement-receipt
--DM1-Sb0636VXPQNO3412Ygegb9suqr
Content-Disposition: form-data; name="input-format" Content-Type:
text/plain;charset=ISO-8859-1 Content-Length: 3
X12
--DM1-Sb0636VXPQNO3412Ygegb9suqr
Content-Disposition: form-data; name="input-data"; filename="TEST-DTEBS_846_3563_10142015102600.x12"
Content-Type:
multipart/encrypted;boundary=boundary2--extKDvsqGExTQI2WVuplHFCUQJ3XVL protocol:
application/pgp-encrypted
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL
Content-Disposition: form-data; name="version" Content-Type:
application/pgp-encrypted Content-Length: 10
Version: 1
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL
Content-Disposition: form-data; name="payload" Content-Type: application/octet-stream Content-Length:
1118
-----BEGIN PGP MESSAGE----- Version: BCPG v1.47
hQIOA8aa0/bci2e/EAf/fkgOBK+cOoeY+44htcvjE9Sxk9RK73aOEaxDOBNqFOsV
Nzw1va7cc7wzYOTlHhZV/MkpXq+IrZV316moJu1EAhscN9YmyReVDOzrkzZ1hBmq
V2MlAr5/CRqS7nQEmCqy/Mz0STtAPZNg3FOh8F79dJnRws5/SdW+zvn2MoSvGTs3
0YvjmtPq2YyVkMlAvJ62f903UYNIiAcEe1k8gWDVW9LJ4tGud+8H0bsG8EFWPsSI
b5rON+MKGykbWGnbSrOy9cqslJaghj91r7ZbsXXXiRn9v4rpOardfiBEnoamI5Tz
jfDKESnr7CqoZgb62je+NJrB5puoAwy8LTynXx4cmQgAnf8lb6f+mIxa1U1Mu6iR
KNTjrH83Cx2BjTra8E8hu0O7GvuczGfA9NiONmxbglgKK58/QVYdlsZUiGTaWkkY
4nMHiaFe9TiPQ6W2vMGutLFR3V6YSshchCskl6cP1OdaIXCYgzZKIjUWHePzd/ds
zCZeNqul1N7iIQ0L5lfSGgfB2eDeeK3JKFjetrrbJQXz0+XeTZLPMSMr6W5Ggq3l
PnoJVTrxUd3b2WKgR4FZB+imYRWEz5KLnB5ZVedC+fU24bECcNptrTQaZZUWnR3Z
ABJqz8avBrNV0Jf9BaFRxcWkrWyr4KtxDArxXOuMHNvGVtj+hhPZcPGj3N9Xam3E
6snAGebIiAEivZPYLVVVk0lZW2H0ISwkKkjD8qs7h8wa7Qbchp2K5IyENEzi20oe
FNRoCwHyjsCY0nECHCG3WON0EXG2DZDTvA17OoyfuKnRT09FVt3OOEPTV048vUid
kUoGa7uT7ud9En7uNSfmDpw+xHxB38N986BKBtoeJlGgDYISJkIgr40TIKMLE/Yc
whDnmKxT68dfSGcv/DIUzYs+d/OBzSpHIWK87me5QnhmLTQ1xwfwtFoTyk6B3Vzx
ljlZFD6e7PJfjo/RIhb0zMvtBvRTONRrMXKRdRY=
=serM
-----END PGP MESSAGE-----
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL--
--DM1-Sb0636VXPQNO3412Ygegb9suqr--
Embedded content from Map:
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL
Content-Disposition: form-data; name="version" Content-Type: application/pgp-encrypted Content-Length:
10
Version: 1
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL
Content-Disposition: form-data; name="payload" Content-Type: application/octet-stream Content-Length:
1118
-----BEGIN PGP MESSAGE----- Version: BCPG v1.47
hQIOA8aa0/bci2e/EAf/fkgOBK+cOoeY+44htcvjE9Sxk9RK73aOEaxDOBNqFOsV
Nzw1va7cc7wzYOTlHhZV/MkpXq+IrZV316moJu1EAhscN9YmyReVDOzrkzZ1hBmq
V2MlAr5/CRqS7nQEmCqy/Mz0STtAPZNg3FOh8F79dJnRws5/SdW+zvn2MoSvGTs3
0YvjmtPq2YyVkMlAvJ62f903UYNIiAcEe1k8gWDVW9LJ4tGud+8H0bsG8EFWPsSI
b5rON+MKGykbWGnbSrOy9cqslJaghj91r7ZbsXXXiRn9v4rpOardfiBEnoamI5Tz
jfDKESnr7CqoZgb62je+NJrB5puoAwy8LTynXx4cmQgAnf8lb6f+mIxa1U1Mu6iR
KNTjrH83Cx2BjTra8E8hu0O7GvuczGfA9NiONmxbglgKK58/QVYdlsZUiGTaWkkY
4nMHiaFe9TiPQ6W2vMGutLFR3V6YSshchCskl6cP1OdaIXCYgzZKIjUWHePzd/ds
zCZeNqul1N7iIQ0L5lfSGgfB2eDeeK3JKFjetrrbJQXz0+XeTZLPMSMr6W5Ggq3l
PnoJVTrxUd3b2WKgR4FZB+imYRWEz5KLnB5ZVedC+fU24bECcNptrTQaZZUWnR3Z
ABJqz8avBrNV0Jf9BaFRxcWkrWyr4KtxDArxXOuMHNvGVtj+hhPZcPGj3N9Xam3E
6snAGebIiAEivZPYLVVVk0lZW2H0ISwkKkjD8qs7h8wa7Qbchp2K5IyENEzi20oe
FNRoCwHyjsCY0nECHCG3WON0EXG2DZDTvA17OoyfuKnRT09FVt3OOEPTV048vUid
kUoGa7uT7ud9En7uNSfmDpw+xHxB38N986BKBtoeJlGgDYISJkIgr40TIKMLE/Yc
whDnmKxT68dfSGcv/DIUzYs+d/OBzSpHIWK87me5QnhmLTQ1xwfwtFoTyk6B3Vzx
ljlZFD6e7PJfjo/RIhb0zMvtBvRTONRrMXKRdRY=
=serM
-----END PGP MESSAGE-----
boundary2----extKDvsqGExTQI2WVuplHFCUQJ3XVL--
Not sure what you mean, but I'd suggest to DEBUG the code in the MultipartAwareFormHttpMessageConverter.readMultipart(). That is a place where an UploadedMultipartFile appears.
A quick look at the CommonsMultipartResolver seems to indicate it doesn't support nested multiparts.
It might not be too hard to subclass it and override parseRequest(), but you might have to dig into commons-fileupload as well.
EDIT
Regarding multipart response:
I just patched the http sample and found that, as long as the request has a proper accept header, simply returning a MultiValueMap "just works".
If you need more control over the part headers, you would need to subclass the AllEncompassingFormHttpMessageConverter and override getHttpEntity() to insert extra headers and configure the gateway to use the custom adapter. Basic headers work ok, though - this...
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("foo", "Hello");
map.add("bar", "baz");
map.add("fiz", "qux".getBytes());
return map;
...results in this...
--6_zk8PXoaiUO_By2XJuUpjs5CEEnYM-wj0qE
Content-Disposition: form-data; name="foo"
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 5
Hello
--6_zk8PXoaiUO_By2XJuUpjs5CEEnYM-wj0qE
Content-Disposition: form-data; name="bar"
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 3
baz
--6_zk8PXoaiUO_By2XJuUpjs5CEEnYM-wj0qE
Content-Disposition: form-data; name="fiz"
Content-Type: application/octet-stream
Content-Length: 3
qux
--6_zk8PXoaiUO_By2XJuUpjs5CEEnYM-wj0qE--

WebApi 2.1 PUT throw error 415

I'm trying to update data using WebApi PUT method. My code working fine before, but suddenly I start to get this error.
"Message":"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'xEmployee' from content with media type 'application/octet-stream'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException".
This is headers:
Response Header.
HTTP/1.1 415 Unsupported Media Type
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
Set-Cookie: Role=D65520F37D105E39C1A92C15CD482E378F32A769592AC7D8305285A5B9B90362F7F2F13F14E6DC220E44D26940B06B52E7460EF13184F245805AF9523D1072464F4BD06AFB4F8AEB8B7D8BF607A8922C6041A3A4C636BF3B26388E606A94FE43; expires=Tue, 07-Oct-2014 09:49:56 GMT; path=/
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 07 Oct 2014 09:19:56 GMT
Content-Length: 809
Request Header:
PUT /api/xemployees/2110481232 HTTP/1.1
Host: guideonline.ilvestour.office
Connection: keep-alive
Content-Length: 229
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://guideonline.ilvestour.office
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Content-Type: application/json; charset=UTF-8"
Referer: http://guideonline.ilvestour.office/account
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: .ASPXAUTH=215C424A0A023F5B42775B7A73B08FEC8CB36E7200FBA430EADF2F300A84500571F8B5EE980C3EF2913FE160978973CDBC50BDD216E16FC342EF0B566D0944ECFD901DF471DEF9F6E5D272B52F2450CC0A1FB96BCC6B3B6E7A7C07343D4DFBD66; Role=DE678EE89D7089B8CD74B202E00C53CA9AE9E4C40B506C5C4EEF56E7962F38ED86F6BFD34E5FD3A6DD6ECCCF61AF768CAB0C1D7C5F15A8638F9454B24DF3208F021EB638235420574C6420CA5A19F0B6BD07BAC303FF79612D6C1AF246563A7
Request Payloadview source
{"Kod":2110481232, "Сотрудник": "Lena", "Telephon": "088-6734227", "Password":"rimosa57", "email":"samoylova-elena#mail.ru", "CrWho":"OMEGA.Administrator", "CrWhen":"2014-10-07T09:20:05.735Z"}
Nothing special in Controller code:
[Authorize(Roles = "Admin, User")]
public async Task<IHttpActionResult> PutxEmployee(int id, xEmployee xEmployee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != xEmployee.Kod)
{
return BadRequest();
}
try
{
var user = db.xEmployee.Find(id);
user.Сотрудник = xEmployee.Сотрудник;
user.Telephon = xEmployee.Telephon;
user.Password = xEmployee.Password;
user.email = xEmployee.email;
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!xEmployeeExists(id))
{
return NotFound();
}
else
{
throw;
}
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
var path = "C:/error.txt";
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
}
using (StreamWriter sw1 = File.CreateText("C:/error1.txt"))
foreach (var ve in eve.ValidationErrors)
{
sw1.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
return StatusCode(HttpStatusCode.NoContent);
}
Same as WebApiConfig:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.Culture = new CultureInfo("ru-RU");
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
There seems to be a typo (last ") in the request's Content-Type header:
Content-Type: application/json; charset=UTF-8"
When this header is missing or malformed, the server will automatically use application/octet-stream by default, as described by this post.
I were facing similar problem. Though i have a Content-Type declare it was still sending same error.
What I did is added Accept Header and it started working.
Accept: application/json
Content-Type: application/json
Also, make sure there's only one header Content-Type. In my case, my rest client was implicitly sending the empty Content-Type which got overridden by Content-Type: application/json hence the error.

WebAPI 2 and post parameters in body passed as null

I have a webapi2 controller method as below:
[Route("shipment/{shipmentId:long}/quotes/register")]
public HttpResponseMessage ProvideQuote(long shipmentId, [FromBody]RegisterQuote quote)
{
HttpResponseMessage response;
response = Request.CreateResponse(HttpStatusCode.Accepted, String.Format("quote of price {0} for shipment {1} has been registred", quote.QuotePrice, shipmentId));
return response;
}
now the issue is that when I test the method using my REST client, the quote is always null.
here is how I am testing
parameter
now the response I get is:
as you can see that the query string parameter is being passed correctly, but the body parameter is not passed. any idea what am I doing wrong.
Edit 1:
Request Header:
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Authorization: Bearer Hhz54k1BLyPMJxucSeq4pLwhS3Y4Ez5WCoEMzhe-uH7gHMFMRjHE2Im9juinMLhqaHVZmkVrWetEiCsYbaduzLas7rYf-D3p40lH_q3IDEn2rdt122qpiHvnUr7Cz2b6GXiPYLGDMQFOMN0lbkYmoZe95sxXDRvDfpdJw4G2Fk3Ri1A25F3qAZCnBhjA-BLoL-2eAjxX-RPCGAXjaNLjT4zsxRJH8NP5qC7azPrWCDKRuK282hnTbKViQjMBDflwlxdPhTNkiCBtxWn03xRcxH9GD1z5Ca0Qinn5gUS7qWwCt9zoZtHcbwFY1kvxyx7x5yCuyEfrGHgKG1s7zjTPjNwU0eV7cC6xQA2GsOAnqADxMDyRryCRKLY7WcyQftRhZ70WbtSW2PI0F7qmDr8n0wvktmKglusLEDf4ib925n-ajVTyMl7v9O-9OsdgCj_GSNE6_bszd3Ak1yUurs-VoQ
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,kn;q=0.6
BTW I am not sure what happened to all my images though!
Regards
Kiran
The issue here is that you are sending Json content in the body but have the content-type header as application/x-www-form-urlencoded...change the content-type header to application/json or text/json if want to send data in json...

Resources