Android and Retrofit HTTP POST yields HTTP 400 : Bad Request

EIBUser <[email protected]> Mon, 18 Mar 2019 11:33:25 -0700 (PDT)
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
------=_Part_606_1455436752.1552934005891
Content-Type: multipart/alternative; 
	boundary="----=_Part_607_1211350941.1552934005892"

------=_Part_607_1211350941.1552934005892
Content-Type: text/plain; charset="UTF-8"

When running a Retrofit-based HTTP POST, the Cherrypy server responds with 
HTTP 400
However, when the same request is run with POSTMAN, the Cherrypy server 
processes the request.

Some details:

The request:

public void sendPost(String title, String body) {
    mAPIService.savePost(title, body, 19).enqueue(new Callback<Post>() {
        @Override
        public void onResponse(Call<Post> call, Response<Post> response) {
            if (response.code() == 400) {
                Log.v("Error code 400",response.message().toString());
                Log.v("Error code 400",response.headers().toString());
                Log.v("Error code 400",response.errorBody().toString());



The verbose Android log:

2019-03-18 14:22:57.638 32433-32433/com.example.android.retrofitrestfulapi V/Error code 400: Bad Request
2019-03-18 14:22:57.639 32433-32433/com.example.android.retrofitrestfulapi V/Error code 400: Content-Type: text/html;charset=utf-8
    Server: CherryPy/14.0.0
    Date: Mon, 18 Mar 2019 18:22:59 GMT
    Content-Length: 2430
    Access-Control-Allow-Origin: *





The Interface: APIService.java contains:


    @Headers({"Content-Type: application/json", "Accept: application/json" , "Access-Control-Allow-Origin: *", })
    @POST("/userprofileinfo")
    @FormUrlEncoded
    Call<Post> savePost(@Field("title") String title,
                        @Field("body") String body,
                        @Field("userId") long userId);
}


Happy to include the full code but wondered if anyone has seen this occur where the Cherrypy server returns HTTP 400


-- 
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.

------=_Part_607_1211350941.1552934005892
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">When running a Retrofit-based HTTP POST, the Cherrypy serv=
er responds with HTTP 400<div>However, when the same request is run with PO=
STMAN, the Cherrypy server processes the request.</div><div><br></div><div>=
Some details:</div><div><br></div><div>The request:</div><div><pre style=3D=
"color: rgb(0, 0, 0); font-family: Menlo; font-size: 9pt;"><span style=3D"c=
olor:#000080;font-weight:bold;">public void </span>sendPost(String title, S=
tring body) {<br>    <span style=3D"color:#660e7a;font-weight:bold;">mAPISe=
rvice</span>.savePost(title, body, <span style=3D"color:#0000ff;">19</span>=
).enqueue(<span style=3D"color:#000080;font-weight:bold;">new </span>Callba=
ck&lt;Post&gt;() {<br>        <span style=3D"color:#808000;">@Override<br><=
/span><span style=3D"color:#808000;">        </span><span style=3D"color:#0=
00080;font-weight:bold;">public void </span>onResponse(Call&lt;Post&gt; cal=
l, Response&lt;Post&gt; response) {<br>            <span style=3D"color:#00=
0080;font-weight:bold;">if </span>(response.code() =3D=3D <span style=3D"co=
lor:#0000ff;">400</span>) {<br>                Log.<span style=3D"font-styl=
e:italic;">v</span>(<span style=3D"color:#008000;font-weight:bold;">&quot;E=
rror code 400&quot;</span>,response.message().toString());<br>             =
   Log.<span style=3D"font-style:italic;">v</span>(<span style=3D"color:#00=
8000;font-weight:bold;">&quot;Error code 400&quot;</span>,response.headers(=
).toString());<br>                Log.<span style=3D"font-style:italic;">v<=
/span>(<span style=3D"color:#008000;font-weight:bold;">&quot;Error code 400=
&quot;</span>,response.errorBody().toString());</pre><pre style=3D"color: r=
gb(0, 0, 0); font-family: Menlo; font-size: 9pt;"><br></pre><pre style=3D"c=
olor: rgb(0, 0, 0); font-family: Menlo; font-size: 9pt;"><br></pre><pre sty=
le=3D"color: rgb(0, 0, 0); font-family: Menlo; font-size: 9pt;">The verbose=
 Android log:</pre><pre style=3D""><font color=3D"#000000" face=3D"Menlo"><=
span style=3D"font-size: 12px;">2019-03-18 14:22:57.638 32433-32433/com.exa=
mple.android.retrofitrestfulapi V/Error=C2=A0code=C2=A0400: Bad Request
2019-03-18 14:22:57.639 32433-32433/com.example.android.retrofitrestfulapi =
V/Error=C2=A0code=C2=A0400: Content-Type: text/html;charset=3Dutf-8
    Server: CherryPy/14.0.0
    Date: Mon, 18 Mar 2019 18:22:59 GMT
    Content-Length: 2430
    Access-Control-Allow-Origin: *</span><span style=3D"font-size: 9pt;"><b=
r></span></font></pre><pre style=3D""><font color=3D"#000000" face=3D"Menlo=
"><span style=3D"font-size: 12px;"><br></span></font></pre><pre style=3D"">=
<font color=3D"#000000" face=3D"Menlo"><span style=3D"font-size: 12px;"><br=
></span></font></pre><pre style=3D""><font color=3D"#000000" face=3D"Menlo"=
><span style=3D"font-size: 12px;"><br></span></font></pre><pre style=3D""><=
font color=3D"#000000" face=3D"Menlo"><span style=3D"font-size: 12px;"><br>=
</span></font></pre><pre style=3D""><font color=3D"#000000" face=3D"Menlo">=
<span style=3D"font-size: 12px;">The Interface: APIService.java contains:</=
span></font></pre><pre style=3D""><font color=3D"#000000" face=3D"Menlo"><s=
pan style=3D"font-size: 12px;"><br></span></font></pre><pre style=3D""><pre=
 style=3D"color: rgb(0, 0, 0); font-family: Menlo; font-size: 9pt;">    <sp=
an style=3D"color:#808000;">@Headers</span>({<span style=3D"color:#008000;f=
ont-weight:bold;">&quot;Content-Type: application/json&quot;</span>, <span =
style=3D"color:#008000;font-weight:bold;">&quot;Accept: application/json&qu=
ot; </span>, <span style=3D"color:#008000;font-weight:bold;">&quot;Access-C=
ontrol-Allow-Origin: *&quot;</span>, })<br>    <span style=3D"color:#808000=
;">@POST</span>(<span style=3D"color:#008000;font-weight:bold;">&quot;/user=
profileinfo&quot;</span>)<br>    <span style=3D"color:#808000;">@FormUrlEnc=
oded<br></span><span style=3D"color:#808000;">    </span>Call&lt;Post&gt; s=
avePost(<span style=3D"color:#808000;">@Field</span>(<span style=3D"color:#=
008000;font-weight:bold;">&quot;title&quot;</span>) String title,<br>      =
                  <span style=3D"color:#808000;">@Field</span>(<span style=
=3D"color:#008000;font-weight:bold;">&quot;body&quot;</span>) String body,<=
br>                        <span style=3D"color:#808000;">@Field</span>(<sp=
an style=3D"color:#008000;font-weight:bold;">&quot;userId&quot;</span>) <sp=
an style=3D"color:#000080;font-weight:bold;">long </span>userId);<br>}</pre=
><pre style=3D"color: rgb(0, 0, 0); font-family: Menlo; font-size: 9pt;"><b=
r></pre><pre style=3D"color: rgb(0, 0, 0); font-family: Menlo; font-size: 9=
pt;">Happy to include the full code but wondered if anyone has seen this oc=
cur where the Cherrypy server returns HTTP 400</pre></pre><pre style=3D""><=
font color=3D"#000000" face=3D"Menlo"><span style=3D"font-size: 12px;"><br>=
</span></font></pre></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher=
rypy-users+unsubscribe-/[email protected]</a>.<br />
To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl=
egroups.com">cherrypy-users-/[email protected]</a>.<br />
Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use=
rs">https://groups.google.com/group/cherrypy-users</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

------=_Part_607_1211350941.1552934005892--

------=_Part_606_1455436752.1552934005891--