post request - errors

Occasional Contributor

Hello Everyone,

I created URL for post request by using MSFlow. 

In share point framework, I am sending a post request and just expecting response by writing the below lines of code

 private _getListData(): Promise<any> {
const spOpts:ISPHttpClientOptions= {headers: { 'Content-Type': 'value' }, body:"value" };
const postURl = "

";
return this.context.spHttpClient
.post(postURl, SPHttpClient.configurations.v1, spOpts)
.then((response: any) => {
console.log('Post request is success', response);
return response.json();
});
}

 

When i check the URL in Postman, the post request is working okay and i am receiving the response back. but When i try that URL in share point framework(HelloWorldWebPart.ts),  I get error

Uncaught (in promise) Error: Unable to determine web URL
at Function.e.getWebUrlFromRequestUrl (sp-webpart-workbench-assembly_en-us_8c5f51c6434ef13aebeff59cb3564410.js:19)
at sp-webpart-workbench-assembly_en-us_8c5f51c6434ef13aebeff59cb3564410.js:19

 

Please could some one help me to fix this error, i am not sure where i am doing it incorrect. 

 

2 Replies

@Vani_metamicro 

Did you ever figure this out?  I'm having the same issue in my code.

Hi @Vani_metamicro ,

 

SPHttpClient class getWebUrlFromRequestUrl(requestUrl) method attempts to infer the SPWeb URL associated with the provided REST URL, by looking for common SharePoint path components such as "_api", "_layouts", or "_vit_bin". So, if the requestUrl is "http://example.com/_layouts/service", the returned URL would be "http://example.com".

 

But here, the url generated is 

const postURl = "

";

 

So, it could not infer any SPWebUrl from it. So, instead of SPHttpClient, use HttpClient class. Also, the flow will be triggered by a HttpRequest.

 

The modified code is:

private _getListData(): Promise<any> {
const spOpts:IHttpClientOptions= {headers: { 'Content-Type': 'value' }, body:"value" };
const postURl = "

";
return this.context.HttpClient
.post(postURl, HttpClient.configurations.v1, spOpts)
.then((response: any) => {
console.log('Post request is success', response);
return response.json();
});
}

 

For more info, refer https://docs.microsoft.com/en-us/javascript/api/sp-http/sphttpclient?view=sp-typescript-latest