简体   繁体   中英

Translating specific cUrl request into/with Java

I had an issue: I am trying to convert a curl request into java. Thusfar, I have tried the makeCurl JAR files as well as tried directly using Apache http. However, I can't figure out the conversion. I am new to both Java and Curl , so I know most of the issue is a lack of familiarity with the API. However, as this is time sensitive, I would really appreciate the help turning this relatively simple line of code into java.

curl \
  -H 'Authorization: Bearer O3YPXEJHQ6TWNFI7HIBEGLVDBZA2SEKN' \
  'https://api.wit.ai/message?q=my_message'

Which returns:

{

  "msg_id" : "e0d5c96d-ca14-43a8-bcf9-993e5c717155",

  "msg_body" : "my_message",

  "outcome" : {
    "intent" : "greetings",
    "entities" : { },
    "confidence" : 0.525

  }

If I could get this return back into a string, I could finish the rest of the program. This one step has been a pain though. Thank you so much in advance for the help!

-Andrew

Update:

Okay: I can now send the request, and it is being received by the server, though I am using the entirely wrong code. I tried the java.net.URL/URLConnection library, and used this:

package curlHttp;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class curlStackTest {

    public static void main(String args[]) throws IOException {
        String stringUrl = "https://api.wit.ai/message?q=test";
        URL url = new URL(stringUrl);
        URLConnection uc = url.openConnection();

        uc.setRequestProperty("Authorization", "Bearer O3YPXEJHQ6TWNFI7HIBEGLVDBZA2SEKN");


        InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());

        // read this input

    }
}

Now i just need to figure out how to read the output coming back from the cUrl request...

Try with the following code

Scanner scanner = new Scanner(inputStreamReader);

while(scanner.hasNextLine()){
    System.out.println(scanner.nextLine());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM