API Post call returns "Failed to Fetch"

%3CLINGO-SUB%20id%3D%22lingo-sub-81504%22%20slang%3D%22en-US%22%3EAPI%20Post%20call%20returns%20%22Failed%20to%20Fetch%22%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-81504%22%20slang%3D%22en-US%22%3E%3CP%3E%26gt%3B%20I%20have%20created%20a%20SPFx%20Web%20Part%20using%20React.%3C%2FP%3E%3CP%3E%26gt%3B%20It%20implict%20authenticates%20and%20acquires%20access%20token.%3C%2FP%3E%3CP%3E%26gt%3B%20When%20i%20am%20calling%20graph%20api%20for%20GET%20it%20works%20fine.%3C%2FP%3E%3CP%3E%26gt%3B%20When%20i%20am%20calling%20graph%20api%20for%20POST%20call%20for%20creating%20user.%20It%20is%20creating%20user%20in%20AD%20as%20well%20as%20throws%20error%20of%20%22Failed%20to%20Fetch%22%20and%20not%20getting%20updated%20user%20object%20in%20response.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFind%20code%20below%20to%20call%20this%20api.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%3Estatic%3C%2FSPAN%3E%20%3CSPAN%3EcreateADUser%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3EaccessToken%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3Estring%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EhttpClient%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3EHttpClient%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EobjUser%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3Estring%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3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ttpClient%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Epost%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3E%60%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fusers%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fusers%3C%2FA%3E%60%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EHttpClient%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Econfigurations%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Ev1%3C%2FSPAN%3E%3CSPAN%3E%2C%20%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eheaders%3A%3C%2FSPAN%3E%3CSPAN%3E%20%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E'Content-type'%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3E'application%2Fjson%3Bodata.metadata%3Dnone'%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E'Authorization'%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3E'Bearer%20'%3C%2FSPAN%3E%20%3CSPAN%3E%2B%3C%2FSPAN%3E%20%3CSPAN%3EaccessToken%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ebody%3A%3C%2FSPAN%3E%20%3CSPAN%3EobjUser%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E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)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%3CSPAN%3Ereturn%3C%2FSPAN%3E%3C%2FSPAN%3E%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CP%3EDoes%20anyone%20know%20about%20it%3F%20What%20may%20be%20the%20cause%20of%20it%20and%20what%20will%20be%20the%20solution%3F%3C%2FP%3E%3CP%3E%26nbsp%3BThank%20you%20in%20advance.%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-81910%22%20slang%3D%22en-US%22%3ERe%3A%20API%20Post%20call%20returns%20%22Failed%20to%20Fetch%22%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-81910%22%20slang%3D%22en-US%22%3E%3CP%3EYes%20its%20working%20fine%20with%20graph%20explorer.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20also%20creates%20user%20but%20not%20returning%20response.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-81577%22%20slang%3D%22en-US%22%3ERe%3A%20API%20Post%20call%20returns%20%22Failed%20to%20Fetch%22%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-81577%22%20slang%3D%22en-US%22%3E%3CP%3EHave%20you%20tried%20with%20Graph%20Explorer%3F%20Does%20this%20work%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdeveloper.microsoft.com%2Fen-us%2Fgraph%2Fgraph-explorer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdeveloper.microsoft.com%2Fen-us%2Fgraph%2Fgraph-explorer%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

> I have created a SPFx Web Part using React.

> It implict authenticates and acquires access token.

> When i am calling graph api for GET it works fine.

> When i am calling graph api for POST call for creating user. It is creating user in AD as well as throws error of "Failed to Fetch" and not getting updated user object in response.

 

Find code below to call this api.

 

private static createADUser(accessToken: string, httpClient: HttpClient, objUser: string): Promise<IUser> {
return new Promise<IUser>((resolve: (newUser: IUser) => void, reject: (error: any) => string): void => {
httpClient.post(`https://graph.microsoft.com/v1.0/users`, HttpClient.configurations.v1, {
headers: {
'Content-type': 'application/json;odata.metadata=none',
'Authorization': 'Bearer ' + accessToken,
},
body: objUser
})
.then((response: HttpClientResponse): Promise<any> => {
return response.json();
}).then((objUser: any) => {
resolve(objUser);
}, (error: any): string => {
var strError: string = error.message;
if (strError.toLowerCase() == "failed to fetch")
resolve(null);
else
reject(strError);
return;
});
});
}
 

Does anyone know about it? What may be the cause of it and what will be the solution?

 Thank you in advance.

2 Replies

Have you tried with Graph Explorer? Does this work:

 

https://developer.microsoft.com/en-us/graph/graph-explorer

Yes its working fine with graph explorer.

 

It also creates user but not returning response.