Skip to main content
pshashipreetham
19-Tanzanite
June 22, 2022
Solved

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

  • June 22, 2022
  • 2 replies
  • 2094 views

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.

Best answer by pshashipreetham

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

pshashipreetham
19-Tanzanite
June 22, 2022

 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);

Shashi Preetham || +91 8099838801 || shashi@psptechhub.com
pshashipreetham
pshashipreetham19-TanzaniteAuthorAnswer
19-Tanzanite
June 23, 2022

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);
Shashi Preetham || +91 8099838801 || shashi@psptechhub.com