0

I am trying to create a classic subweb using a POST REST call in SPFx. Any idea what I am doing wrong here?

  const postURL: string = `${this.props.context.siteUrl}/_api/web/webinfos/add`;

  const requestBody: any = {
     'parameters': {
     '__metadata': {
     'type': 'SP.WebInfoCreationInformation'
     },
     'Url': this.props.context.siteUrl + '/' + title,
     'Description': 'Subsite created from REST API',
     'Title': title,
     'Language': 1033,
     'WebTemplate': 'B7A00BA4-AFDC-4E70-B137-10DAEBED4D8B#TemplateName',
     'UseUniquePermissions': true
    }
    };

   const requestHeaders: any = {
    'Accept': 'application/json;odata=verbose',
    'Content-type': 'application/json;odata=verbose',
    'odata-version': '3.0',
    "X-HTTP-Method": "POST"
   };

  const spOpts: ISPHttpClientOptions = {
    headers: requestHeaders,
    body: JSON.stringify(requestBody)
  };

 console.log("About to make REST API request.");

 this.props.context.spHttpClient.post(
 postURL,
 SPHttpClient.configurations.v1,
 spOpts);

Error message: Uncaught (in promise) SyntaxError: Unexpected end of JSON input

0

You should be using the Title value itself in the Url parameter. It doesnt accept full url.

Secondly, you dont need to use requestHeaders. It will be taken care of by the framework itself.

Modify the code as below:

const postURL: string = `${this.props.context.siteUrl}/_api/web/webinfos/add`;

#modify this as per your code
const title = "TestNew5";

const spOpts: ISPHttpClientOptions = {
  body: `{
    "parameters":{
          "@odata.type": "#SP.WebInfoCreationInformation",
          "Title": "${title}", 
          "Url": "${title}",
          "Description": "Subsite created from REST API",
          "Language": 1033,
          "WebTemplate": "B7A00BA4-AFDC-4E70-B137-10DAEBED4D8B#TemplateName",
          "UseUniquePermissions": true
        }
      }`
};

this.props.context.spHttpClient.post(
      postURL,
      SPHttpClient.configurations.v1,
      spOpts).then((response: SPHttpClientResponse) => {

      response.json().then((responseJSON: JSON) => {
          console.log(responseJSON);
      });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.