none
Receiving a 400 / Bad Request Response Attempting an HTTP PUT into a RESTful API RRS feed

  • Question

  • I am getting back an HTTP 400 back attempting to upload a JSON string to a REST API. Things work well if I call the POST URL replace the Method with POST. But, when I use the PUT URI and replace the Method with PUT I am receiving the error back.

                Dim strReturnJsonString As String = ""
                Dim strURL As String
                Dim WebReq As HttpWebRequest
                Dim WebResp As HttpWebResponse
                Dim Encoding As New System.Text.UTF8Encoding
                Dim Reader As StreamReader

                Dim getData__1 As String = ""
                getData__1 = getData__1 & strJSON
                Dim data As Byte() = Encoding.GetBytes(getData__1)

                strURL = "the URI"

                WebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
                WebReq.ContentType = "application/json; charset=utf-8"
                WebReq.ContentLength = data.Length
                WebReq.Method = "PUT"
                WebReq.KeepAlive = True

                '-----------------------------------------------------

                WebReq.Headers.Add("Authorization", "Basic override characters go here...")

                If WebReq.Proxy IsNot Nothing Then
                    WebReq.Proxy.Credentials = CredentialCache.DefaultCredentials
                End If

                '-----------------------------------------------------

                Dim DtaStream As Stream = WebReq.GetRequestStream()

                If data.Length > 0 Then
                    DtaStream.Write(data, 0, data.Length)
                    DtaStream.Close()
                End If

                '----------------------------------------------------- Receive

                WebResp = DirectCast(WebReq.GetResponse(), HttpWebResponse)
                Reader = New StreamReader(WebResp.GetResponseStream())
                strReturnJsonString = Reader.ReadToEnd()

    Thursday, March 2, 2017 4:49 PM

Answers

  • Edward, Thanks for the response... 400 code response turned out to be related to my code attempting to forward a field that was not supported by the server. Once I backed out that field the 400 error went away.

    I did not have much to go one except for the guy handling the API asked me to forward my JSON string which made me wonder about one of the fields. I removed it on chance and it turned out to be the issue.

    Thanks again!

    Jeff

    Friday, March 3, 2017 3:04 AM

All replies

  • You need to use Fiddler (free) and look at the raw URL when you see the line with the 400  error, which is usually due to some bad parm being passed on the URL that is causing the site to be not found on a 400 return code.
    Thursday, March 2, 2017 4:54 PM
  • Hi Jeff,

    What do you mean by “replace the Method with PUT”? Do you mean it did not work when you send request with PUT method? If so, to check whether it is related with your client code, I suggest you try Fiddler to send request to see whether it will work.

    If it did not work under Fiddler, I suggest you try to add below configuration in your WCF Rest Service.

      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">      
          <remove name="WebDAVModule"/>      
        </modules>
        <handlers>
          <remove name="WebDAV"/>
        </handlers>    
      </system.webServer>

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 3, 2017 2:54 AM
  • Edward, Thanks for the response... 400 code response turned out to be related to my code attempting to forward a field that was not supported by the server. Once I backed out that field the 400 error went away.

    I did not have much to go one except for the guy handling the API asked me to forward my JSON string which made me wonder about one of the fields. I removed it on chance and it turned out to be the issue.

    Thanks again!

    Jeff

    Friday, March 3, 2017 3:04 AM