I have a Curl request like:
curl -u "key:value" -H "headers" https://example.com
So, when I try to create a Rest client using this curl request in Java I am confused where to send the -u data in my request. Do we need to send it in Header or as URL parameter. Can somebody help me and tell me how can I send this -u in my Java code?
This is the code I am using:
URL url = new URL("https://example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Headers", "Value");
***conn.setRequestProperty("u", "key:Value");***
The header Authorization: Basic base64encoded(user:pass) works for this question.
Related
I am not familiar with https requests so please take it easy on me.
I want to make a post call and retrieve a token for a url. The url is something like:
/auth/token?param1=value1¶m2=value2¶m3=value3¶m4={{value4}}¶m5={{value5}}
I make the post
HttpURLConnection conn = (HttpURLConnection) authentication.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestMethod("POST");
but in order to get the response I have to send the parameters.
I've tried to write the url params as string to the connection outputstream but it doesnt work.
Any help is appreciated.
I'm having a hard time using the Java HttpURLConnection. Ive tried using HttpClient but that also failed.
All I want to do is this:
curl -H "Authorization: Bearer 1111111111" https://company.aha.io/api/v1/features/APP-1
1111111111 is the API key.
This is my code so far.
URL url = new URL("https://company.aha.io/api/v1/features/APP-1");
connection = (HttpsURLConnection) url.openConnection();
//add request header for authentication
connection.setRequestProperty("Authorization", "Bearer " + token);
connection.connect();
System.out.println(connection.getResponseCode());
I keep getting a 404 code.
Thank you for the help.
I am trying to send XML over http url using HttpURLConnection.
Following is the test curl command that I am using to test the service.
curl -H "Content-Type: text/xml" -H "User-Agent: some/7.88/1" -X POST
--Basic -u "username:password" -d '< ?xml packet ... >' http://ip:port/some/url
It is working fine, but when I try to send this with following Java code:
Java Code:
URL url = new URL("http://ip:port/some/url");
String requestXMLPacket = "<?xml packet ... >";
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/xml");
conn.setRequestProperty("User-Agent", "some/7.88/1");
conn.setRequestProperty("basic -u", "username:password");
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setUseCaches(false);
conn.setDoInput(true);
I get an error:
java.io.IOException: Server returned HTTP response code: 401 for URL:
http://ip:port/some/url
Which means that I am not sending correct authorization.
Can you tell me what I am doing wrong here.
Please try adding authentication header as follows
String encoded = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
conn.setRequestProperty("Authorization", "Basic " + encoded);
PS. It uses Java 8 Base64 encoder
How to convert the below curl command to Java using HttpURLConnection?
curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "https://example.com:8081/rest/api/2/issue/QA-31"
How to set headers:
HttpURLConnection myCon = (HttpURLConnection) new URL("https://example.com:8081/rest/api/2/issue/QA-31").openConnection();
myCon.setRequestMethod("GET");
myCon.setRequestProperty("Content-Type","application/json");
myCon.setRequestProperty("Authorization", "Basic ZnJlZDpmcmVk");
After that you can read URL content in two ways
a. you can get InputStream from Connection as
InputStream inStream = myCon.getInputStream();
and read it as you want directly or with some reader etc.
b. by getting Content as
Object content = myCon.getContent();
For details read Java Docs for those methods it is very well documented.
BTW: keep in mind you have an SSL connection then you have to have remote server certificate in your java Key Store.
I am creating a django rest api, and I'm trying to send JSON data via PUT request from an Android device, using HttpUrlConnection.
URL url = new URL(myurl);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("PUT");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
Log.v("Apiput", MainActivity.cookieManager.getCookieStore().getCookies().get(0).toString());
conn.connect();
if(conn.getResponseCode() != 200) {
return "" + conn.getResponseCode();
}
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
osw.write(put);
osw.flush();
osw.close();`
I know I have to send a csrf token, but I think that I'm sending it already.
By examining the META in my request I can see the csrf token both in headers and cookies:
'HTTP_COOKIE': 'csrftoken=3jLNzfLIu1P5dBH4WWwggHMH7oDQC7Rx;'
And in my android device i have a CookieManager that says that the csrf cookie has the same value.
V/Apiputīš csrftoken=3jLNzfLIu1P5dBH4WWwggHMH7oDQC7Rx
I am getting a 403 (Forbidden) Http error besides the user is authenticated (I can make GET Requests)
[26/Sep/2015 00:16:04]"PUT /api/works/34/ HTTP/1.1" 403 58
With curl I am able to send the request without any problem, with the same user credentials.
I wonder if anyone can tell me what am I doing wrong.
Thanks.
You don't have to set the cookie if you're doing a JSON call to Django REST framework.
It would definitively help if you can provide the permissions associated to the view.