0

I'm getting an error says Unexpected char 0x0a at 18 in header value when I add a custom header to my Volley String request.

Here is my code.

stringRequest = new StringRequest(Request.Method.GET, URL, string -> {
            try {
                JSONObject jsonObject = new JSONObject(string);
                Log.d("TAG", jsonObject.toString());
            } catch (Exception e) {

            }
        }, error -> {

        }){
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String>  params = new HashMap<>();
                params.put("MIME Type", "application/x-www-form-urlencoded; charset=UTF-8");
                params.put("compiler", "c");
                params.put("code", code);
                params.put("input", "");
                return params;
            }
        };

the error I'm getting.

2020-10-07 10:52:06.869 32119-32232/com.my.app E/Volley: [2415] NetworkDispatcher.processRequest: Unhandled exception java.lang.IllegalArgumentException: Unexpected char 0x0a at 18 in header value: #include <stdio.h>
    
    int main()
    {
int foo = 0;
    
printf("Hello world");
    
return foo;
    }
    java.lang.IllegalArgumentException: Unexpected char 0x0a at 18 in header value: #include <stdio.h>
    
    int main()
    {
int foo = 0;
    
printf("Hello world");
    
return foo;
    }
        at com.android.okhttp.Headers$Builder.checkNameAndValue(Headers.java:319)
        at com.android.okhttp.Headers$Builder.add(Headers.java:250)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.addRequestProperty(HttpURLConnectionImpl.java:600)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.addRequestProperty(DelegatingHttpsURLConnection.java:187)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.addRequestProperty(HttpsURLConnectionImpl.java:30)
        at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:95)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)

2 Answers2

0

0x0A is \n newline character. make sure in your header don't have "\n".

I think in your header you have some line! and you can send code with body!

aref behboodi
  • 164
  • 2
  • 11
0

Solved it from this answer

• Step 1: removed params.put("MIME Type", "application/x-www-form-urlencoded; charset=UTF-8");

• Step 2: replaced params.put("code", code); with params.put("code", Base64.encodeToString(code.getBytes(), Base64.NO_WRAP));

UPDATE

• I have removed step 2 from my code because it was causing me a problem. So it now looks like params.put("code", code);