The remote server returned an error: (400) Bad Request.

carlos_navarro
New Member
5 0 0

Hello,

I'm currently using .NET to send out requests to add new products.
We are able to read products without any issues at the moment but writing is not working.

 

Details:

The app is a private app.

Here's the style of the url we are using.
https://itgift.myshopify.com/admin/products.json

Here’s the JSON:
{ product: "{"title":"Product 1","description":"Description of product 1"}"}

 

We are using .NET 4.0 and upon using httpwebresponse we get:
The remote server returned an error: (400) Bad Request.

Here's the code we are using
 

 /// <summary>
        /// The core executor for sending off requests
        /// </summary>
        string ExecuteRequest(string url, string verb, string data) {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.PreAuthenticate = true;
            var creds = new NetworkCredential(userName: _apiKey, password: _password);
            request.Credentials = creds;

            request.ContentType = "application/json";
            request.Method = verb;
            

            
            

            //add the data if needed
            if (!String.IsNullOrEmpty(data)) {
                using (var ms = new MemoryStream()) {
                    using (var writer = new StreamWriter(request.GetRequestStream())) {
                        writer.Write(data);
                        writer.Close();
                    }
                }
            }

            var response = (HttpWebResponse)request.GetResponse();
            string result = "";

            using (Stream stream = response.GetResponseStream()) {
                StreamReader sr = new StreamReader(stream);
                result = sr.ReadToEnd();
                sr.Close();
            }
            return result;
        }

 

0 Likes
Chris_Saunders
Shopify Staff
Shopify Staff
582 0 49

You don't have enough information in your model that you are sending up. I believe you are missing some of the more important bits like price and such.

While it is a 400 error, you should be able to read the body of the response which contains information about the things that were missing in your request.

Also can you verify that the code is a 422 which means that your request was an unprocessable entity (i.e. you don't have all the data you need)

carlos_navarro
New Member
5 0 0

Hi Chris,

Here's the JSON that I'm now using. Same error 400. I validated the JSONoutput by going to jsonlint.com.
I also do not see any other error code response besides 400.

 

{
    " product ": "{\"variants\":{\"option1\":\"First\",\"price\":\"10.00\",\"sku\":\"123\"},\"title\":\"Product 1\",\"body_html\":\"<strong>Good snowboard!</strong>\",\"vendor\":\"Burton\",\"product_type\":\"Snowboard\"}"
}

 

0 Likes
Chris_Saunders
Shopify Staff
Shopify Staff
582 0 49

Your JSON bodies are invalid.

Yes it is valid JSON but it's not valid for what Shopify is expecting.

You are effectively sending

{ product: "a really long string of stuff"}

But what you need to be sending is

{
  "product":  {
    "title":"Product 1",
    "description":"Description of product 1"
  }
}

You want to take your Hash object or whatever and convert that to JSON at the last possible moment. You are probably converting your data to JSON then setting it as a key on your Hash, that you then convert again to JSON or something.

carlos_navarro
New Member
5 0 0

Hi Chris,

 

You are correct. I can't believe I missed that. Thank you! 

Here's the final code. I've removed my key, password and site.

 

string newProduct =
            "{" +
                "\"product\": {" +
                    "\"title\": \"T-Shirt\"," +
                    "\"body_html\": \"Limp Bisquit\"," +
                    "\"vendor\": \"Burton2\"," +
                    "\"product_type\": \"T-Shirt\"," +
                    "\"variants\": " +
                         "[{ \"option1\": \"First\", \"price\": \"30.00\", \"sku\": \"123\" }]" +
                "}" +
            "}";


            string url = "https://yourstore.myshopify.com/admin/products.json";;
            string verb = "POST";
            string data = newProduct;

            ExecuteRequest(url, verb, data, "[apikey]", "[apiPassword]");




            var query = shopify.products();
            foreach (var product in query.products)
            {
                //System.Diagnostics.Debug.WriteLine(product.title);
                Console.WriteLine(product.title);
            }
			

		// <summary>
        /// The core executor for sending off requests
        /// </summary>
        string ExecuteRequest(string url, string verb, string data, string apikey, string password)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.PreAuthenticate = true;
            var creds = new NetworkCredential(userName: apikey, password: password);
            request.Credentials = creds;

            request.ContentType = "application/json";
            request.Method = verb;
            //request.Method = WebRequestMethods.Http.Post;
            request.Accept = "*/*";
            //request.ContentLength = 200; 

            //add the data if needed
            if (!String.IsNullOrEmpty(data))
            {
                using (var ms = new MemoryStream())
                {
                    using (var writer = new StreamWriter(request.GetRequestStream()))
                    {
                        writer.Write(data);
                        writer.Close();
                    }
                }
            }





            //var response = (HttpWebResponse)request.GetResponse();
            var response = request.GetResponse() as HttpWebResponse;

            string result = "";

            using (Stream stream = response.GetResponseStream())
            {
                StreamReader sr = new StreamReader(stream);
                result = sr.ReadToEnd();
                sr.Close();
            }
            return result;
        }

 

0 Likes
Chris_Saunders
Shopify Staff
Shopify Staff
582 0 49

Surely the .NET framework has a way of coercing HashMaps into JSON objects. If Java can do it with libraries like Jackson or GSON, there's gotta be a .NET equivalent.

I'd definitely suggest you move over to using a tool for your JSON conversion. You'll be far happier and have fewer goofy bugs :)

0 Likes
carlos_navarro
New Member
5 0 0

Hi Chris,

For some reason now my product on_hand is not getting updated.
Are there some fields that need to be declared, such as updated_at before it can get updated?

Here's what I'm reading on: http://docs.shopify.com/api/product
No errors, just not getting the quantity updated.

 

 

0 Likes
carlos_navarro
New Member
5 0 0

Okay. After reading a little closer at the documentation, I noticed that a variant ID is different from the product ID.

The following format worked properly. The url is as mentioned in the document. /admin/variant/388109922.json

 

{ "variant" : {"id":"388109922","inventory_quantity":100}}

 

0 Likes
oneill_aloha
New Member
2 0 0

Hi Chris,

While we requesting the create refund api we are getting the 'The remote server returned an error: (400) Bad Request'.

Here is the my url:
https://flip-critts.myshopify.com/admin/orders/3770046414953/refunds.json

Here is the my JSON:-

{
"refund": {
"restock": "true",
"notify": "true",
"currency": "USD",
"shipping": {
"full_refund": false
},
"refund_line_items": [

],
"transactions": {
"amount": "0",
"kind": "refund"
}
}
}

Here is the my Request ID:
X-Request-ID: 6f2673ef-aa5b-46fa-b910-4eb4afc3d983

Could you please tell us where we are missing.

Thanks,

0 Likes