Support

Account

Home Forums Backend Issues (wp-admin) How to send a post request with custom fields to the wordpress api

Unread

How to send a post request with custom fields to the wordpress api

  • I’m trying to send a post request to the rest api with some custom fields. THis is my code

    `let newCharacter = {
    ‘title’: $(‘.create-char-name’).val(),
    ‘acf’: {
    ‘char_class’: $(‘#char-class’).val(),
    ‘char_subclass’: $(‘#char-subclass’).val(),
    ‘char_level’: $(‘#char-level’).val()
    },
    ‘status’: ‘publish’
    }

    $.ajax({
    beforeSend: (xhr) => {
    xhr.setRequestHeader(‘X-WP-Nonce’, spbk_data.nonce);
    },
    url: spbk_data.root_url + ‘/wp-json/wp/v2/character/’,
    method: ‘POST’,
    data: newCharacter,
    success: (response) => {
    console.log(“congrats”);
    console.log(response);
    },
    error: (response) => {
    console.log(“Sorry”);
    console.log(response);
    }
    });`

    The request goes through without any problems, but when I check the json, the “acf” field returns false.
    I’m using the acf to wp api plugin, if that information is useful.

    The only info I found about this issue was this post, but I don’t really understand what the answer meant. I tried adding xhr.setRequestHeader('Content-Type', application/json); (I also tried with lower case initials), below the nonce, like the post seems to suggest, but that returns this error:

    "{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}"

Viewing 1 post (of 1 total)

The topic ‘How to send a post request with custom fields to the wordpress api’ is closed to new replies.