Node.js http.ClientRequest: get raw headers - cookies

I am using Node.js 0.2.3 and response.headers['set-cookie'] seems to be truncated after the first cookie. Is there any way I can just read the raw headers?
BTW, the set-cookie header should contain:
id1=sw34rwdsfsd;secure;
id2=wer235sd2354;secure;
id3=df435df4543;secure
My guess would be it is not parsing the boolean attributes right and stops after the first one. Anyone know if this is fixed in later versions of Node.js (even though I can't upgrade just yet)?

var spawn = require('child_process').spawn;
function getHeader(url, callback){
var client = spawn('curl', ['-I', url]);
client.stdout.setEncoding('***');
client.stdout.on('data', function(data){
callback(data);
});
}
The -I flag asks curl for just the header. Pass whatever encoding to setEncoding - I think it defaults to the raw that you're looking for.

Related

C++ Boost http post request [duplicate]

I've to use a C++ library for sending data to a REST-Webservice of our company.
I start with Boost and Beast and with the example given here under Code::Blocks in a Ubuntu 16.04 enviroment.
The documentation doesn't helped me in following problem:
My code is, more or less, equal to the example and I can compile and send a GET-request to my test webservice successfully.
But how can I set data inside the request (req) from this definition:
:
beast::http::request<beast::http::string_body> req;
req.method("GET");
req.target("/");
:
I tried to use some req.body.???, but code completition doesn't give me a hint about functionality (btw. don't work). I know that req.method must be changed to "POST" to send data.
Google doesn't show new example about this, only the above code is found as a example.
Someone with a hint to a code example or using about the Beast (roar). Or should I use websockets? Or only boost::asio like answered here?
Thanks in advance and excuse my bad english.
Small addition to Eliott Paris's answer:
Correct syntax for setting body is
req.body() = "name=foo";
You should add
req.prepare_payload();
after setting the body to set body size in HTTP headers.
To send data with your request you'll need to fill the body and specify the content type.
beast::http::request<beast::http::string_body> req;
req.method(beast::http::verb::post);
req.target("/");
If you want to send "key=value" as a "x-www-form-urlencoded" pair:
req.set(beast::http::field::content_type, "application/x-www-form-urlencoded");
req.body() = "name=foo";
Or raw data:
req.set(beast::http::field::content_type, "text/plain");
req.body() = "Some raw data";

Cannot set expire in private cookie Rocket-Rust

I'm trying to setting expire in private Cookie in Rust (Rocket Framework version 0.5.0-rc.1) using rocket::http::Cookie.
In rocket add_private doc I read:
Unless a value is set for the given property, the following defaults are set on cookie before being added to self:
path: "/"
SameSite: Strict
HttpOnly: true
Expires: 1 week from now
I don't understand how to set the Expires property.
I tried to create a new cookie and setting expire using .set_expires() (as in doc example), but it gives me the error: "the trait From<OffsetDateTime> is not implemented for std::option::Option<time::offset_date_time::OffsetDateTime>". The code that return the error is something like (values here only for test purpose):
use rocket::http::{Cookie, CookieJar};
use cookie::time::{Duration, OffsetDateTime};
fn handler(jar: &CookieJar<'_>) {
let mut cookie = Cookie::new("name", "value");
let mut now = OffsetDateTime::now_utc();
now += Duration::days(1);
cookie.set_expires(now);
jar.add_private(cookie);
}
I wonder if I have to use cookie crate instead of rocket::http to create the cookie, but in that case I cannot use CookieJar in the response handler because it expected a rocket::http::Cookie and not a cookie::Cookie.
Is there any other way to set an expire or a max age in private cookie using Rocket http module?
I encountered this same problem and I think I've figured it out:
Rocket uses both the cookie and time packages internally
The example code in the Rocket docs for rocket::http::Cookie actually comes from the cookie package and therefor confusingly uses use cookies::Cookie; instead of use rocket::http::Cookie.
Key thing: The docs at https://api.rocket.rs/v0.5-rc/ appear to be newer that the code that's in crates.io, and use different versions of the cookie and time crates.
So you need to use the same version or cookie or time that Rocket is using. If you're using rocket 0.5.0-rc.1 from crates.io then you need cookie 0.15 or time 0.2.11.
I was able to get my code working with these lines in my Cargo.toml:
rocket = { version = "0.5.0-rc.1", features = ["secrets", "json"] }
time = "0.2.11"
Your example code would then become:
use rocket::http::{Cookie, CookieJar};
use time::{Duration, OffsetDateTime};
fn handler(jar: &CookieJar<'_>) {
let mut cookie = Cookie::new("name", "value");
let mut now = OffsetDateTime::now_utc();
now += Duration::days(1);
cookie.set_expires(now);
jar.add_private(cookie);
}
Alternatively, I think if you use version 0.15 of cookie (you might be using 0.16.0-rc.1) then your code should work as you have it. I think it's cleaner to import Duration and OffsetTime directly from the time package though.
It looks like the latest version of Rocket in Github exposes time as rocket::time so we should be able to switch to that when final 0.5 release comes out.

Run prerequest script to stringify body of the request

I am using post man to send a post request with the body as form-data which contains files and text. See the image below:
I want to json.stringify the entire body but I cannot work out how to do this in a pre-request script. As an environment variable can only be one part of the body further having files makes it more tricky.
I am not sure I understand the problem. In postman the request is a JavaScript object. If you are trying to stringify the request, I assume you are trying to get this:
propertyOne=valueOne&propertyTwo=ValueTwo
out of this:
const request = {
propertyOne: 'valueOne',
propertyTwo: 'ValueTwo'
};
The simple way is just to iterate the object's properties and write into an string:
function stringifyRequest(object) {
let resultString = '';
for (var property in object) {
if (object.hasOwnProperty(property)) {
let tempString = `${property}=${object[property]}`;
resultString = resultString ? `${resultString}&${tempString}` : tempString;
}
}
return resultString
}
Now, if you want to get the binary of the file you are uploading, it will not be possible. As seen in this thread:
We don't give access to the contents of the files in pre-request
scripts, for a few reasons.
We want to delay loading file contents to right before the request
is sent.
The request body is not actually resolved until the pre request
scripts are completed. So even if we wanted to we can't give the
actual body of the request in pre-request scripts.
They may eventually change that, but I could not find any indications of it. One user in this thread suggests using insomnia, you could check it out if fits your needs better.

how to get the path of cookies using ssjs?

I am trying to get the path of a cookie
I have several cookies with the same name but with different paths.
The following example return null using the getpath() method
var request = facesContext.getExternalContext().getRequest();
var cookies = request.getCookies();
print(cookies[1].getName()))
print(cookies[1].getPath()))
I also tried the global object "cookie" in xpages but how do I get the path from that?
How can I get the path of all the cookies using ssjs?
You can't. Googled a bit. Seems to be a common problem beyond Domino and XPages. Both getDomain and getPath return null, because browser does not send it to the server. Explained here getDomain() of javax.servlet.http.Cookie is returning null
Also a short thought here https://coderanch.com/t/283519/java/Cookie-getDomain-returns-null that it may be security feature.
Both ways of getting a cookie return a java.servlet.http.Cookie, which does have the getPath() method, which you're using. Are you sure the path is getting set? Looking at an POST request from an XPage (both via the servlet it's calling and FireBug), the DomAuthSessId and SessionID cookies don't have a path set, so getPath() returns null.

soap connector with access token from header

I'm using the loopback-connector-soap and can pass my access token in like this:
var ds = loopback.createDataSource('soap',
{
...
,soapHeaders: ["..."+ token +"..."]
});
I'm putting a REST layer on top of this and I got it working. But 3rd parties will be hitting this API, so what I really need is to allow the third party to pass their token in via the header when they hit the REST route:
Authorization: Bearer _token_
The app will then place their token in the soap header. Does loopback's soap-connector allow for this scenario?
Things to try:
The loopback token module can be instructed to look for values in headers that you specify: http://apidocs.strongloop.com/loopback/#loopback-token
app.use(loopback.token({
cookies: ['foo-auth'],
headers: ['foo-auth', 'X-Foo-Auth'],
params: ['foo-auth', 'foo_auth']
}));
I use it myself for other scenarios (need it in my remote methods): https://github.com/ShoppinPal/warehouse/blob/master/server/server.js#L17, but if that doesn't "just work" meaning if that doesn't directly translate into the value also being set into the soap-connector automagically ...
Then perhaps you can use a middleware to take the value and set into the loopback context, to be later picked up by your soap connector? Here's some (crude) middleware code of mine: https://github.com/ShoppinPal/warehouse/blob/master/server/server.js#L18-L35
... but I wonder where you might write code for the soap-connector to pick that value out of the loopback context? Because right now the instantiation looks to be global and one time so I wonder when you get a chance again to edit it.