问题
Following is what I want to implement using JMeter: I want to make a request to an API that implements OAuth signing. The API makes a POST request with a binary file.
I am trying to use OAuth Request sampler plugin. This plugin, unlike HTTP Request sampler, doesn't have 'Send Files with the Request' option.
Is there some way I can still implement it?
回答1:
To people who might not be familiar with jmeter. Here is sample code. Add a beanshell sampler and write in Java to sign the request and inject the authorization header into the http request sampler afterwards.
Here is the code for beanshell sampler
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.http.client.methods.HttpPost;
log.info("start of signing the request");
String consumerKey = "[consumerKey]";
String consumerSecret ="[consumerSecret]";
String token = "[token]";
String secret = "[secret]";
OAuthConsumer consumer;
consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
consumer.setTokenWithSecret(token, secret);
HttpPost request = new HttpPost("[url]");
consumer.sign(request);
System.out.println(request.getFirstHeader("Authorization").toString());
String oauth = request.getFirstHeader("Authorization").toString().substring(15);
vars.put("oauth" ,oauth);
return oauth;
回答2:
Did you try authenticating first using OAuth sampler then sending the file using regular http sampler.
来源:https://stackoverflow.com/questions/25048339/post-a-file-in-jmeter-with-oauth-signing