Metin Ilhan
Metin Ilhan

Reputation: 241

Can't post param value

Following code helps to post data to server. It was working very well but now I have to send a string named as token.

I also used different ways like to send params but still I get error code

400: bad request

All I get is that token value somehow causes some problems. If you try code it will send message like result will be:

{"durum":"hata","mesaj":"token_gerekli"} it is supposed to be something like {"durum":"hata","mesaj":"token_zaman_Aşımı"}

 @Override
    protected Void doInBackground(String... params)
    {
        try
        {
            String link = "http://cvbenim.com/api/v1/isveren/uyeliktamamla";
            URL url = new URL(link);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestProperty("User-Agent", "");
            connection.setRequestMethod("POST");
            connection.setDoInput(true);

            String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQsImlzcyI6Imh0dHA6XC9cL3Byb2plLmFwcFwvYXBpXC92MVwvZ2lyaXMiLCJpYXQiOjE0NTk5NTA5NTQsImV4cCI6MTQ1OTk3NjE1NCwibmJmIjoxNDU5OTUwOTU0LCJqdGkiOiIwMzRhYmEyY2JmYWEyODg4ZmZjY2ZiZjAxZDA3OTI1YyJ9.Saan9lSUb3FWeFfSNWO4hKyFU-osca0T32CdjC-9Kd8";
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("token",token)
                    .appendQueryParameter("firma_adi","SomeName")
                    .appendQueryParameter("sektor", "1")
                    .appendQueryParameter("sehir", "2")
                    .appendQueryParameter("ilce", "3")
                    .appendQueryParameter("semt","4")
                    .appendQueryParameter("adres","SomePlace")
                    .appendQueryParameter("telefon","02122342111")
                    .appendQueryParameter("email", "[email protected]")
                    .appendQueryParameter("web","dogu.com");


            String query = builder.build().getEncodedQuery();
            OutputStream os = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();

            connection.connect();
            InputStream is ;
            if(connection.getResponseCode()>=400)
                is=connection.getErrorStream();
            else
                is = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            writer.close();
            os.close();
            is.close();
            String result = sb.toString();
            Log.e("Final Result ",result);

            Message msg = new Message();
            msg.obj = result;
            mh.sendMessage(msg);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

Upvotes: 0

Views: 201

Answers (2)

Metin Ilhan
Metin Ilhan

Reputation: 241

I found solution it was some server problem I sent token with GET and other params with usual Post it works. In case somebody need it here is my solution.

           String link = "http://cvbenim.com/api/v1/isveren/uyeliktamamla?token="+token;

           URL url = new URL(link);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestProperty("User-Agent", "");
            connection.setRequestMethod("POST");
            connection.setDoInput(true);

            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("firma_adi","SomeName")
                    .appendQueryParameter("sektor", "1")
                    .appendQueryParameter("sehir", "2")
                    .appendQueryParameter("ilce", "3")
                    .appendQueryParameter("semt","4")
                    .appendQueryParameter("adres","SomePlace")
                    .appendQueryParameter("telefon","02122342111")
                    .appendQueryParameter("email", "[email protected]")
                    .appendQueryParameter("web","dogu.com");


            String query = builder.build().getEncodedQuery();
            OutputStream os = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();

            connection.connect();

Upvotes: 1

Tigger
Tigger

Reputation: 9120

I get the following response:

{"durum":"hata","mesaj":"token_gerekli"}

With the following code:

<html>
<head>
</head>
<body>
<form action="http://cvbenim.com/api/v1/isveren/uyeliktamamla" method="post">
<input type="hidden" name="token" value="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQsImlzcyI6Imh0dHA6XC9cL3Byb2plLmFwcFwvYXBpXC92MVwvZ2lyaXMiLCJpYXQiOjE0NTk5NTA5NTQsImV4cCI6MTQ1OTk3NjE1NCwibmJmIjoxNDU5OTUwOTU0LCJqdGkiOiIwMzRhYmEyY2JmYWEyODg4ZmZjY2ZiZjAxZDA3OTI1YyJ9.Saan9lSUb3FWeFfSNWO4hKyFU-osca0T32CdjC-9Kd8"
<input type="hidden" name="firma_adi" value="SomeName" />
<input type="hidden" name="sektor" value="1" />
<input type="hidden" name="sehir" value="2" />
<input type="hidden" name="ilce" value="3" />
<input type="hidden" name="semt" value="4" />
<input type="hidden" name="adres" value="SomePlace" />
<input type="hidden" name="telefon" value="02122342111" />
<input type="hidden" name="email" value="[email protected]" />
<input type="hidden" name="web" value="dogu.com" />
<input type="submit" />
</form>
</body>
</html>

So this points to something wrong with your reading of the API or the connection or something else. That is, you are getting the correct response from the cvbenim.com API based on what you are passing it (well, posting to it really).

Upvotes: 0

Related Questions