3

I'm making a POST request using Retrofit + Okhttp, and I'm running into the following error:

02-05 04:45:13.981  15972-16249/com.myapp.android D/Retrofit﹕ ---> HTTP POST http://10.0.0.4:3000/api/v1/users/1/posts
02-05 04:45:13.981  15972-16249/com.myapp.android D/Retrofit﹕ Accept: application/json
02-05 04:45:13.981  15972-16249/com.myapp.android D/Retrofit﹕ Content-Type: application/json; charset=UTF-8
02-05 04:45:13.981  15972-16249/com.myapp.android D/Retrofit﹕ Content-Length: 150
02-05 04:45:13.981  15972-16249/com.myapp.android D/Retrofit﹕ {"description":"test","image_url":"https://s3.amazonaws.com/bucket/xxx-4800-b0e0-fc206f95f158.jpeg","title":"test","price":0.0,"user_id":0}
02-05 04:45:13.981  15972-16249/com.myapp.android D/Retrofit﹕ ---> END HTTP (150-byte body)
02-05 04:45:14.001  15972-15972/com.myapp.android W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
02-05 04:45:14.017  15972-16249/com.myapp.android D/Retrofit﹕ ---- ERROR http://10.0.0.4:3000/api/v1/users/1/posts
02-05 04:45:14.017  15972-16249/com.myapp.android D/Retrofit﹕ java.net.ProtocolException: Unexpected status line: HTTP/1.1 422��Unprocessable Entity
            at com.squareup.okhttp.internal.http.StatusLine.parse(StatusLine.java:73)
            at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
            at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
            at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:791)
            at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:90)
            at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:784)
            at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:645)
            at com.squareup.okhttp.Call.getResponse(Call.java:263)
            at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:219)
            at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:192)
            at com.squareup.okhttp.Call.execute(Call.java:79)
            at retrofit.client.OkClient.execute(OkClient.java:53)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
            at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
            at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
            at retrofit.RxSupport$2.run(RxSupport.java:55)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at retrofit.Platform$Android$2$1.run(Platform.java:142)
            at java.lang.Thread.run(Thread.java:841)
02-05 04:45:14.017  15972-16249/com.myapp.android D/Retrofit﹕ ---- END ERROR

Using the following libs:

compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'

I'm expecting something like the following to come back (intentionally testing a 422 error):

{
    "title": [
        "is too short (minimum is 6 characters)"
    ]
}

I get the expected result when sending via Postman (https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en)

Edit: I'm making the following call in Rails to return this 422 result:

render json: @post.errors.to_json, status: :unprocessable_entity

Another Edit:

Here's a screenshot of the request being made via Wireshark. Nothing looks out of the ordinary, but I've also never used the tool before.

Overall

Here's the appropriate hex.

Hex

loeschg
  • 29,961
  • 26
  • 97
  • 150
  • 3
    i have same error. did you find a solution? – savepopulation Oct 13 '15 at 17:44
  • @savepopulation unfortunately no. Fortunately it just started working. ¯\\_(ツ)_/¯ Yeah... I know that's a terrible answer, but it's the truth :) – loeschg Oct 15 '15 at 15:28
  • when i checked server logs there's an unhandled exception which causes returning not a valid response from server. so retrofit gets an status line exception. – savepopulation Oct 15 '15 at 20:39

1 Answers1

7

Your server appears to violate the HTTP spec. The status line is defined as follows:

The first line of a response message is the status-line, consisting of the protocol version, a space (SP), the status code, another space, a possibly empty textual phrase describing the status code, and ending with CRLF.

status-line = HTTP-version SP status-code SP reason-phrase CRLF

From the exception it appears that a non-space character (or two) is between the status code and reason phrase.

OkHttp is looking for ASCII spaces (character #32) between these elements. You can use something like WireShark or maybe Charles to intercept the network traffic and figure out what characters are being used by your HTTP server.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • That's strange. I'm not doing anything out of the ordinary with Rails (not much more than a "Hello World" sort of app. I'll take a look using what you suggested and see if it sheds any light. – loeschg Feb 05 '15 at 18:51
  • Yeah I'd be curious to know what's happening here. I don't think I've seen something like this before. Getting the hex values of those characters would help identify. I can also go spelunking in Rails eventually, but not right now. – Jake Wharton Feb 05 '15 at 19:00
  • I definitely saw this return correctly before. I'm having a hard time tracking down what I changed since or what variables would have been introduced. I added the line of code that *should* be generating this response in my original question. – loeschg Feb 05 '15 at 19:48
  • I have wireshark up and running. I updated my question with more info. – loeschg Feb 13 '15 at 05:08
  • Huh. That hex looks completely reasonable. Did that response end up producing the same error in the client? – Jake Wharton Feb 13 '15 at 05:39
  • It did, yeah. Same result/error. Is there something I might be doing wrong client side? I think I'm doing a pretty typical Observable call. – loeschg Feb 13 '15 at 12:35