/orders/cart/v1 - 500 Internal Server Error

Discussion in 'Shapeways API' started by 493737_deleted, Mar 16, 2014.

  1. Hi,

    I am currently having a problem adding a model to a user's cart on my website. When I reach the request.GetResponse() line, I receive an internal server error from the API.

    The code I'm using is posted below. It's based off the code posted on the Github site.

    I've verified that the JSON is valid and I am using model #835118 which is used in this example: https://www.shapeways.com/tutorials/shoppingcart

    I'm not exactly sure where I'm going wrong with this - any help would be appreciated. If you need any more information please let me know.


    Code:
    
    public object AddShoppingCartItem(int modelID, int materialID, int quantity = 1)
            {
                OAuthRequest client = new OAuthRequest()
                {
                    Method = "POST",
                    Type = OAuthRequestType.ProtectedResource,
                    SignatureMethod = OAuthSignatureMethod.HmacSha1,
                    ConsumerKey = this.consumerKey,
                    ConsumerSecret = this.consumerSecret,
                    RequestUrl = "https://api.shapeways.com/orders/cart/v1", 
                    Version = "1.0a",
                    Realm = "shapeways.com",
                    TokenSecret = this.OAuthSecret,
                    Token = this.OAuthToken
                };
                string auth = client.GetAuthorizationQuery();
                string requestUrl = client.RequestUrl + "?" + auth;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
                request.ContentType = "application/json; charset=utf-8";
                request.Method = "POST";
    
                CartItem cartItem = new CartItem
                {
                    materialId = materialID,
                    modelId = modelID,
                    quantity = quantity
                };
                
                using(StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    string json = JsonConvert.SerializeObject(cartItem);
                    writer.Write(json);
                    writer.Flush();
                    writer.Close();
    
                }
    
                
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
                return JsonConvert.DeserializeObject(content);
    
    
            }
    
    
     
    Last edited: Mar 16, 2014
  2. I received some help from the Shapeways API team. Just in case anyone comes across this post, here's what's going on.

    When there's any problem with processing at all, the API will return error code 500. However, in ASP.NET if error code 500 is found, it raises an exception. In order to get the JSON response you need to wrap it in a double try/catch.

    Here's an example of how to perform a POST to the Shapeways API in ASP.NET:


    Code:
    
    
            private object MakeShapewaysPostRequest(string url, string json)
            {
                OAuthRequest client = new OAuthRequest()
                {
                    Method = "POST",
                    Type = OAuthRequestType.ProtectedResource,
                    SignatureMethod = OAuthSignatureMethod.HmacSha1,
                    ConsumerKey = this.consumerKey,
                    ConsumerSecret = this.consumerSecret,
                    RequestUrl = url, 
                    Version = "1.0a",
                    Realm = "shapeways.com",
                    TokenSecret = this.OAuthSecret,
                    Token = this.OAuthToken
                };
                string auth = client.GetAuthorizationQuery();
                string requestUrl = client.RequestUrl + "?" + auth;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
                request.ContentType = "application/json; charset=utf-8";
                request.Method = "POST";
    
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(json);
                    writer.Flush();
                    writer.Close();
                }
    
                HttpWebResponse response = null;
    
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException ex)
                {
                    // Shapeways returns error 500 on any processing issues.
                    // ASP throws exception on error 500. To get around this,
                    // wrap in try/catch to get error response.
                    if (ex.Response != null)
                    {
                        response = (HttpWebResponse)ex.Response;
                    }
                }
    
                string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
                return JsonConvert.DeserializeObject(content);
            }
    
    

    Major thanks to Brett Langdon at Shapeways for the help.