Need Equivalent gatling scala code from postman curl url
could you please just logically write a equivalent gatling scala code for below authentication request,first thing first is i need to get a auth request and its response using gatling scala .Below is the authentication curl request in PostMan which is working fine,i mean giving 201 response ,but i am getting 400 response in Gatling using git url github
Generated from a curl request:
curl 'https://api.platform.com/auth/oauth/token' -H 'Accept: application/json, text/plain, */*' -H 'Referer: https://api-origin.cloud/dev/reservations-web/signin' -H 'Origin: https://api.origin.cloud' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' -H 'Authorization: Basic aWtyd3VnaDM4NzFnaHc4cmduN3E4M2c2c3I=' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'grant_type=password&username=abc.xyc1%40abc.com&password=Password1%21' --compressed
Below is the scala equivalent code i tried generating but getting 400 response
package simulations
import baseConfig.BaseSimulation
import io.gatling.core.Predef._
import io.gatling.core.session
import io.gatling.http.Predef._
import scala.concurrent.duration.DurationInt
import scala.util.Random
class ogrebattledriverPostAccessToken extends BaseSimulation {
exec(
http("HTTP Request auth")
.post("https://api.platform.com/auth/oauth/token")
.header("Content-Type","application/x-www-form-urlencoded")
.formParam("grant_type","password")
.formParam("username", "TableTopEng%40xxxs.com")
.formParam("password", "actual password")
.check(status is 200)
.check(jsonPath("$.access_token").saveAs("access_token"))
.check(jsonPath("$.token_type").saveAs("token_type"))
)
//val PostData=jsonFile("input-json.json").circular
def name() = Random.nextInt(Integer.MAX_VALUE).toString
//val headers_10 = Map("Content-Type" -> """application/json""","Authorization","${token_type} + ${access_token}" )
def getSpecificOgreID()={
repeat(140000){
exec(flushHttpCache)
feed(usersDataSource)
.exec(http("PostRequestPerformanceTestingOgreBattleDriver")
.post("https://api.tabletop-stage.tiamat-origin.cloud/dev/ogre-battledriver/Organizations")
// .headers(headers_10)
.header("Authorization", "${token_type} + ${access_token}")
.body(StringBody(session =>
s"""
|{
| "name": "${name()}",
| "latitude": 66.256538,
| "longitude": -95.934502,
| "phoneNumber": "555-555-5555",
| "emailAddress": "perftest1#perftest1.com",
| "website": "https://perftest1",
| "streetLine1": "123 perftest1.",
| "streetLine2": "Ste 400",
| "city": "Omaha",
| "state": "NE",
| "zipCode": "98002"
|}
""".stripMargin)).asJSON
.check(status.in(200,201))//checkforaspecificstatus
.check(jsonPath(path="$.name").saveAs(key="name")))//checkforaspecificstatus
.exec{session=>println(session);session}//parameterfortheorgIdgoeshere
.pause(1)
}
}
// add a scenario
val scn = scenario("Battle Driver Get Orgs ")
.forever() { // add in the forever() method - users now loop forever
exec(getSpecificOgreID())
}
Error i am getting in Intellij Editor
20:22:56.563 [ERROR] i.g.h.a.s.HttpRequestAction - 'httpRequest-5' failed to execute: No attribute named 'access_token' is defined
Can someone please write a logical gatling scala code from above postman curl request??
Related
I am trying to do the setup for mirror maker 2 using my current connect cluster.
Based on this documentation it can be done via connect rest api.
https://cwiki.apache.org/confluence/display/KAFKA/KIP-382%3A+MirrorMaker+2.0#KIP-382:MirrorMaker2.0-RunningMirrorMakerinaConnectcluster
I followed the sample sending this PUT request :
PUT /connectors/us-west-source/config HTTP/1.1
{
"name": "us-west-source",
"connector.class": "org.apache.kafka.connect.mirror.MirrorSourceConnector",
"source.cluster.alias": "us-west",
"target.cluster.alias": "us-east",
"source.cluster.bootstrap.servers": "us-west-host1:9091",
"topics": ".*"
}
but i am getting a method not allowed response error response.
{
"error_code": 405,
"message": "HTTP 405 Method Not Allowed"
}
The api looks ok if I do a simple GET from the / , returning the version
{
"version": "2.1.0-cp1",
"commit": "bda8715f42a1a3db",
"kafka_cluster_id": "VBo-j1OAQZSN8tO4lMJ0Gg"
}
the PUT method doesnt work, using POST works as the api's documentation shows:
https://docs.confluent.io/current/connect/references/restapi.html#get--connectors
Remove the name of the connector from the url as #cricket_007 suggested, and wrap the config with new element like this:
curl --noproxy "*" -XPOST -H 'Content-Type: application/json' -H 'Accept: application/json' http://localhost:8083/connectors -d'{
"name": "dc-west-source",
"config": {
"connector.class": "org.apache.kafka.connect.mirror.MirrorSourceConnector",
"source.cluster.alias": "dc-west",
"target.cluster.alias": "dc-east",
"source.cluster.bootstrap.servers": "dc-west-cp-kafka-0.domain:32721,dc-west-cp-kafka-1.domain:32722,dc-west-cp-kafka-2.dc.domain:32723",
"topics": ".*"
}
}
' | jq .
I am trying to send a message to slack from Scala using CURL command. It gives error invalid payload
val slackurl = "https://hooks.slack.com/services/<something here>
val cmd = Seq(
"curl",
"-d",
"testdata",
"-H",
"Content-Type: application/x-www-form-urlencoded",
"-X",
"POST",
slackurl
)
cmd.!
i am getting the error "invalid_payload"
or
val cmd = Seq(
"curl",
"-d",
s"#$testData",
"-H",
"Content-Type: application/x-www-form-urlencoded",
"-X",
"POST",
slackurl
)
when I am trying to send some data, the error is : "cant read from the file"
i used the following: Runtime.getRuntime.exec( Array[String]( "curl", "-X", "POST", "-H", "Content-type: application/json", "--data", actData, slackurl ) ) Its working for me now. [space issue in CURL with Runtime
But when i send the a json to slack, message was displayed as "Array".
I am able to build a Jenkins job with its parameters' default values by sending a POST call to
http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters
and I can override the default parameters "product", "suites" and "markers by sending to this URL:
http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=ALL&suites=ALL&markers=ALL
But I saw examples were the parameters can be override by sending a JSON body with new values. I am trying to do that by sending the following json bodies. Neither of them works for me.
{
'product': 'ALL',
'suites': 'ALL',
'markers': 'ALL'
}
and
{
"parameter": [
{
"name": "product",
"value": "ALL"
},
{
"name": "suites",
"value": "ALL"
},
{
"name": "markers",
"value": "ALL"
}
]
}
What JSON to send if I want to override the values of parameters "product", "suites" & "markers"?
I'll leave the original question as is and elaborate here on the various API calls to trigger parameterized builds. These are the calls options that I used.
Additional documentation: https://wiki.jenkins.io/display/JENKINS/Remote+access+API
The job contains 3 parameters named: product, suites, markers
Send the parameters as URL query parameters to /buildWithParameters:
http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=ALL&suites=ALL&markers=ALL
Send the parameters as JSON data\payload to /build:
http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/build
The JSON data\payload is not sent as the call's json_body (which is what confused me), but rater in the data payload as:
json:'{
"parameter": [
{"name":"product", "value":"123"},
{"name":"suites", "value":"high"},
{"name":"markers", "value":"Hello"}
]
}'
And here are the CURL commands for each of the above calls:
curl -X POST -H "Jenkins-Crumb:2e11fc9...0ed4883a14a" http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/build --user "raameeil:228366f31...f655eb82058ad12d" --form json='{"parameter": [{"name":"product", "value":"123"}, {"name":"suites", "value":"high"}, {"name":"markers", "value":"Hello"}]}'
curl -X POST \
'http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=234&suites=333&markers=555' \
-H 'authorization: Basic c2hsb21pb...ODRlNjU1ZWI4MjAyOGFkMTJk' \
-H 'cache-control: no-cache' \
-H 'jenkins-crumb: 0bed4c7...9031c735a' \
-H 'postman-token: 0fb2ef51-...-...-...-6430e9263c3b'
What to send to Python's requests
In order to send the above calls in Python you will need to pass:
headers = jenkins-crumb
auth = tuple of your (user_name, user_auth_token)
data = dictionary type { 'json' : json string of {"parameter":[....]} }
curl -v POST http://user:token#host:port/job/my-job/build --data-urlencode json='{"parameter": [{"name":"xx", "value":"xxx"}]}
or use Python request:
import requests
import json
url = " http://user:token#host:port/job/my-job/build "
pyload = {"parameter": [
{"name":"xx", "value":"xxx"},
]}
data = {'json': json.dumps(pyload)}
rep = requests.post(url, data)
I am trying to execute the curl command through scala for scalatest,
CURL :
curl -H "Content-Type: application/json" --data #/home/examples/demo/demo.json http://localhost:9090/job
which works aas expected while I tried doing it with scala ,like
import scala.sys.process._
val json = getClass.getClassLoader.getResource(arg0).getPath
val cmd = Seq("curl", "-H", "'Content-Type: application/json'","-d", s"#$json","http://localhost:9090/job")
cmd.!
and it produces following error
Expected 'application/json'
You're quoting too much:
Seq("curl", "-H", "'Content-Type: application/json'","-d", s"#$json","http://localhost:9090/job")
// !^! !^!
Should be:
Seq("curl", "-H", "Content-Type: application/json", "-d", s"#$json","http://localhost:9090/job")
// ! !
The reason you need to quote the content-type in the shell is because the shell will break it up if it isn't quoted. Curl doesn't know how to deal with quotes, because quotes aren't its business. Scala won't do any word-splitting, either, because that's your job.
I'm doing a Spring MVC controller and I still get problem with POST operation.
I've read many solutions on stackoverflow without to fix my problem.
My achievement at the moment :
I sent a GET request with an Id and return an Object converted to JSON successfully.
I failed to send a POST request with a JSON body, return = 415 UNSUPPORTED_MEDIA_TYPE
1) I added to my pom.xml the Jackson API : 1.8.5
2) My Spring configuration file:
I added all necessary parts :
viewResolver
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
MappingJacksonHttpMessageConverter
mvc:annotation-driven
scan my controllers
3) My model object is simple : an Account with Id, Name and an amount
#Document
public class Account implements Serializable {
private static final long serialVersionUID = 9058933587701674803L;
#Id
private String id;
private String name;
private Double amount=0.0;
// and all get and set methods
4) and finally my simplified Controller class :
#Controller
public class AdminController {
#RequestMapping(value="/account", method=RequestMethod.POST,
headers = {"content-type=application/json"})
#ResponseStatus( HttpStatus.CREATED )
public void addAccount(#RequestBody Account account){
log.debug("account from json request " + account);
}
#RequestMapping(value="/account/{accountId}", method=RequestMethod.GET)
#ResponseBody
public Account getAccount(#PathVariable("accountId") long id){
log.debug("account from json request " + id);
return new Account();
}
}
5) On client side I've just executed curl commands :
The successfully GET command :
curl -i -GET -H 'Accept: application/json' http://myhost:8080/compta/account/1
The POST command which failed:
curl -i -POST -H 'Accept: application/json' -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account
Any ideas where I'm going wrong?
Well, "UNSUPPORTED_MEDIA_TYPE" should be a hint. Your curl command is actually sending:
Content-Type: application/x-www-form-urlencoded
Simply add explicit Content-Type header and you're good to go:
curl -v -i -POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account
Try this :
curl -i -POST -H "Accept: application/json" -H "Content-type: application/json" -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account