Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 URI of the created resource.
           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 URI of the created resource.
            return response.Headers.Location;
        }
Posted
Updated 22-Jun-17 18:15pm
v3
Comments
Silver Lightning 13-Jun-17 19:38pm    
do anyone knows how to consume this api with multiple parameter? thank you in advance

1 solution

The issue solved by this code:
public async Task<Uri> SaveItemAsync(Item item)
        {
            RunAsync();
            var path = $"api/cart/{item.ID}/item";
            var paramValues = $"?item={item.ProductID}&store=MAC&type=active&quantity={item.Quantity}";
            var query = path + paramValues;
            
            HttpResponseMessage response = await client.PutAsJsonAsync(query, new StringContent(item.ProductID, UnicodeEncoding.UTF8, "application/json"));

            if (response.IsSuccessStatusCode)
            {
                response.EnsureSuccessStatusCode();
            }
            else
            {
                response.ReasonPhrase.ToString();
            }

            // return URI of the created resource.
            return response.Headers.Location;
            
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900