Hi to all,
Good day!
I'm having an issue regarding the PutAsJsonAsync and PostAsJsonAsync in consuming the rest api: "http://afs-test-redis99.afservice.org:8080/site/"
Operations: PUT
Name: addItem
Path: /api/cart/{id}/item
Query: store type
Parameters: item, quantity,key
Request Body: AnyObject(optional)
Example: In my postman, it is working correctly, when I tried to put/add new item:
"http://afs-test-redis99.afservice.org:8080/site/api/cart/51/item?item=COM1&store=PCM&type=active&quantity=5"
But when I tried to implement this in my mvc repository class
public async Task<Uri> SaveItemAsync(Item item)
{
client.BaseAddress = new Uri("http://afs-test-redis99.afservice.org:8080/site/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue( "application/json"));
HttpResponseMessage response = await client.PutAsJsonAsync($"api/cart/{item.ID}/item?",$"item={item.ProductID}&store=PCM&type=active&{item.Quantity}");
response.EnsureSuccessStatusCode();
return response.Headers.Location;
}
The result error is:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: close
Date: Tue, 13 Jun 2017 00:23:21 GMT
Server: Apache-Coyote/1.1
Content-Length: 1062
Content-Language: en
Content-Type: text/html; charset=utf-8
}}
Can anyone assist me what's wrong on my code, sorry I'm new to consuming the web api in my client application. Thank you very much in advance.
What I have tried:
It is working correctly in postman:
"http://afs-test-redis99.afservice.org:8080/site/api/cart/51/item?item=COM1&store=PCM&type=active&quantity=5"
But when I tried to implement this in my mvc repository class (c#)
public async Task<Uri> SaveItemAsync(Item item)
{
client.BaseAddress = new Uri("http://afs-test-redis99.afservice.org:8080/site/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue( "application/json"));
HttpResponseMessage response = await client.PutAsJsonAsync($"api/cart/{item.ID}/item?",$"item={item.ProductID}&store=PCM&type=active&{item.Quantity}");
response.EnsureSuccessStatusCode();
return response.Headers.Location;
}