locked
RestSharp POST to Square Connect not working RRS feed

  • Question

  • User402820130 posted

    I have the following code below but it's not completing. The best I can tell is that the JSON is not correct however I'm not how to get there. Any help would be appreciated. When I run this it's tell me 400 Bad Request.

        Dim Client As New RestSharp.RestClient("https://connect.squareup.com")
        Dim Request As New RestSharp.RestRequest("v1/" & MERCHANTID & "/categories", RestSharp.Method.POST)
        Request.RequestFormat = RestSharp.DataFormat.Json
        Request.AddHeader("Authorization", "Bearer " & ACCESS_TOKEN)
        Request.AddHeader("Accept", "application/json")
        Request.AddHeader("Content-Type", "application/json")
    
        Request.AddParameter("name", "Milkshake")
    
        Dim response As RestSharp.RestResponse = Client.Execute(Request)
        Dim getresponse As String = response.StatusCode

     

    Sunday, January 31, 2016 1:54 PM

All replies

  • User-2057865890 posted

    Hi youngsc,

    The HTTP ‘Error 400: Bad Request’ typically occurs when a web server receives a request that it is unable to understand. The cause of this is often malformed syntax. If the request contains syntax that doesn’t conform to the HTTP protocol, the server will generate the 400 error. There are a wide variety of possible causes of HTTP 400 errors. The error can originate from problems with either the client or the web server. 

    When you called the Web API, you got the 400 Bad Request error. I suggest you make sure that the request URL is correct. I created a sample on my side depend on your description, it was works fine. I could get the correct message. You can refer to the following code.

    Dim Client As New RestSharp.RestClient("http://localhost:41587/")
    Dim Request As New RestSharp.RestRequest("api/values?name={name}", RestSharp.Method.POST)
    Request.RequestFormat = RestSharp.DataFormat.Json
    Request.AddHeader("Authorization", "Bearer ")
    Request.AddHeader("Accept", "application/json")
    Request.AddHeader("Content-Type", "application/json")
    Request.AddParameter("name", "Milkshake", ParameterType.UrlSegment)
    Dim response As RestSharp.RestResponse = Client.Execute(Request)
    Dim Content = response.Content
    Dim getresponse As String = response.StatusCode
    
    [HttpPost]
    public IEnumerable<string> Post(string name = "default")
    {
        return new string[] { "value1", "value2", name };
    }
    

    Best Regards,

    Chris Zhao

    Monday, February 1, 2016 8:54 AM
  • User402820130 posted
    Thanks for the reply. I'll take another look at the url and make sure I'm forming it correctly. Done quite a bit with other aspects of vb and asp but never anything to do with api.
    Monday, February 1, 2016 12:23 PM