Skip to content
This repository was archived by the owner on Aug 12, 2022. It is now read-only.
This repository was archived by the owner on Aug 12, 2022. It is now read-only.

Call the dialog url  #3

@wmashal

Description

@wmashal

Hi
I am trying to send http request to the dialog i succeed with curl but i dotn know why its not working in java code I am always recieve 400 code when I am trying to pass teh message and the conversation_id

This is my code
please advice ?
``public String doApiRequest(String text, String token, String language, URL obj,boolean conversation) throws RecastException {
HttpURLConnection con=null;
OutputStream os;
int responseCode = 0;
String inputLine;
StringBuffer responseBuffer;
String recastJson;

    try {

        con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("POST");
        con.setRequestProperty("Authorization", "Token " + token);
        con.setDoOutput(true);
        os = con.getOutputStream();
        if(conversation){
            JSONObject messageJson = new JSONObject();
            messageJson.put("message","{\"content\":\""+text+"\",\"type\":\"text\"}");
            messageJson.put("conversation_id","fgbotconv123456");
            
            os.write(messageJson.toString().getBytes());
        }

        text = "text=" + text;
        os.write(text.getBytes());

        if (language != null) {
            String l = "&language=" + language;
            os.write(l.getBytes());
        }
        os.flush();
        os.close();

        responseCode = con.getResponseCode();
    } catch (MalformedURLException e) {
        throw new RecastException("Invalid URL", e);
    } catch (IOException e) {
        throw new RecastException("Unable to read response from Recast", e);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if (responseCode == HttpURLConnection.HTTP_OK) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            responseBuffer = new StringBuffer();
            while ((inputLine = reader.readLine()) != null) {
                responseBuffer.append(inputLine);
            }
            reader.close();
        } catch (IOException e) {
            throw new RecastException("Unable to read response from Recast", e);
        }
        recastJson = responseBuffer.toString();
    } else {
        System.out.println(responseCode);
        throw new RecastException(responseCode);
    }
    return recastJson;
}``

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions