Skip to main content
Jun 22, 2022 5:48 PM UTC
Solved

Get Request, how to use form-data with GET Request in the Thingworx

Hi there,

Trying to do the Get Request with the Form Data but that is not working in the TWX. But in the PostMan, it gives me an 200 Ok Response.

Postman Form-Data: 

psp316r_0-1655920050769.png

How this Form data can be used in the Get Request in Thingworx Service.

@slangley 

Thanks,
Shashi.

Jun 23, 2022 11:09 AM UTC

This is fixed by following code 

var TableData = me.GetDataTableEntries({
 maxItems: undefined /* NUMBER {"defaultValue":500} */
});

var content = "grant_type=" + TableData.grant_type + "&client_id=" + TableData.client_id + "&client_secret=" + TableData.client_secret;

params = {
 proxyScheme: "https" /* STRING {"defaultValue":"http"} */ ,
 url: "https://***********" /* STRING */ ,
 content: content /* STRING */ ,
 contentType: "application/x-www-form-urlencoded" /* STRING */ ,
};

// response : STRING
response = Resources["ContentLoaderFunctions"].PostText(params);
result = JSON.parse(response);

2 replies

Jun 22, 2022 7:04 PM UTC

 This is Thingworx Service Code, But doesn't work.

 

var table = Resources["InfoTableFunctions"].CreateInfoTable();
table.AddField({name: "form-data", baseType: "JSON"});
table.AddRow({
 "form-data" : 
 {
 grant_type : "*********",
 client_id : "*********"",
 client_secret : "*********"",
 resource : "*********"" 
 }
});

let params = {
	proxyScheme: "https" /* STRING {"defaultValue":"http"} */,
	headers: {"Content-Type" : "multipart/form-data", "Accept": "*/*"} /* JSON */,
	ignoreSSLErrors: undefined /* BOOLEAN */,
	useNTLM: undefined /* BOOLEAN {"defaultValue":false} */,
	partsToSend: table /* INFOTABLE */,
	workstation: undefined /* STRING {"defaultValue":""} */,
	useProxy: undefined /* BOOLEAN {"defaultValue":false} */,
	repository: undefined /* STRING */,
	proxyHost: undefined /* STRING {"defaultValue":""} */,
	url: "*********"" /* STRING */,
	timeout: undefined /* NUMBER {"defaultValue":60} */,
	proxyPort: undefined /* INTEGER {"defaultValue":8080} */,
	password: undefined /* STRING */,
	pathOnRepository: undefined /* STRING */,
	domain: undefined /* STRING {"defaultValue":""} */,
	username: undefined /* STRING */
};

// result: JSON
let result = Resources["ContentLoaderFunctions"].PostMultipart(params);

pshashipreethamAuthorAnswer
Jun 23, 2022 11:09 AM UTC

This is fixed by following code 

var TableData = me.GetDataTableEntries({
 maxItems: undefined /* NUMBER {"defaultValue":500} */
});

var content = "grant_type=" + TableData.grant_type + "&client_id=" + TableData.client_id + "&client_secret=" + TableData.client_secret;

params = {
 proxyScheme: "https" /* STRING {"defaultValue":"http"} */ ,
 url: "https://***********" /* STRING */ ,
 content: content /* STRING */ ,
 contentType: "application/x-www-form-urlencoded" /* STRING */ ,
};

// response : STRING
response = Resources["ContentLoaderFunctions"].PostText(params);
result = JSON.parse(response);