0

I'm trying to do a super simple fetch to grab some user data for my WP site. This URL works 100% in Postman and I get everything I need. However, in the browser and script, the response is ALWAYS an empty array.

Here's my script:

fetch(url, {
            method: "GET",
            headers: {
                "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjIsIm5hbWUiOiJsemFrb29yIiwiaWF0IjoxNjYzMTA5MzU2LCJleHAiOjE4MjA3ODkzNTZ9.2l957IqslJBsvY9MrV5dT0ykod7kloFCc5qSxGeGlT0",
                "cache-control": "no-cache",
                'Content-Type': 'application/json;charset=UTF-8'
            }
        })
        // Handle success
        .then(response => response.json()) // convert to json
        .then(json => console.log(json)) //print data to console
        .catch(err => console.log('Request Failed', err)); // Catch errors

I'm not sure what I'm doing wrong. I've tried almost everything I can think of. Postman gives me the proper JSON response every time.

The URL is this: /wp-json/wp/v2/users/ Raw:

{"id":668,"name":"008LT","url":"","description":"","link":"author\/008lt\/","slug":"008lt","avatar_urls":{"24":"wp-content\/plugins\/buddyboss-platform\/bp-core\/images\/profile-avatar-buddyboss-50.png","48":"wp-content\/plugins\/buddyboss-platform\/bp-core\/images\/profile-avatar-buddyboss-50.png","96":"wp-content\/plugins\/buddyboss-platform\/bp-core\/images\/profile-avatar-buddyboss-50.png"},"meta":{"project_code":[null],"hours_approved":[],"hours_used":[]},"acf":{"password_was_reset":"yes","acad_coordinator":"Mariah G.","project_poc":"Someone else","student_admin":"Test 1","admin_contact":"Someone","project_id":"11104.0028.001","hd_project_id_status":"","primary_language":"lithuanian (lit)","secondary_language":"russian (rus)","approved_hours":30,"used_hours":15,"todays_appointments":"","scheduled_instructor":"","training_focus":"","jlu":"","let_status":"","cl150_status":"","iltp":"","pmo_account-type":"","hd_account_status":"","tutor_orientation":"","class_activity":"","next_test":"","test_date":"","class_recording":"","last_updated":null,"student_category":"dlt","student-iltp":""},"_links":{"self":[{"href":"wp-json\/wp\/v2\/users\/668"}],"collection":[{"href":"wp-json\/wp\/v2\/users"}],"courses":[{"embeddable":true,"href":"wp-json\/ldlms\/v2\/users\/668\/courses"}],"groups":[{"embeddable":true,"href":"wp-json\/ldlms\/v2\/users\/668\/groups"}],"course-progress":[{"embeddable":true,"href":"wp-json\/ldlms\/v2\/users\/668\/course-progress"}],"quiz_progress":[{"embeddable":true,"href":"wp-json\/ldlms\/v2\/users\/668\/quiz-progress"},{"embeddable":true,"href":"wp-json\/ldlms\/v2\/groups\/668\/quiz-progress"},{"embeddable":true,"href":"wp-json\/ldlms\/v2\/groups\/668\/quiz-progress"}]}}
6
  • you will need to invalidate those tokens now that you've shared them, it's not clear what the URL/endpoint is or the raw response before it's converted to JSON
    – Tom J Nowell
    Commented Sep 15, 2022 at 17:12
  • Sorry about that. I'm still learning quite a few things. I've added the raw response and endpoint below my question
    – lz430
    Commented Sep 15, 2022 at 17:46
  • was that the raw response from postman or the raw response from the JS in the browser? You can see the raw response via the browser dev tools network panel, that's what you need to share ( and make sure it's labelled as such to avoid confusion )
    – Tom J Nowell
    Commented Sep 16, 2022 at 12:41
  • That was Postman's response. The browser is empty every single time. Both the script and when I paste the URL I'm calling directly
    – lz430
    Commented Sep 16, 2022 at 17:07
  • Have you checked the Network panel though? I'm not interested in Postman, or the console.log output, I'm interested in the raw network response that chrome/firefox receives before it's handed to JS. If you're using JS to output the value then that value is tainted. Looking at the request itself in the network panel bypasses any mistakes or missing steps and shows you the raw unprocessed network request. Also keep in mind that you've still not expanded your code snippet to include what came before it. This could be a trivial mistake creating url.
    – Tom J Nowell
    Commented Sep 17, 2022 at 21:35

1 Answer 1

1

The answer for me was setting the X-WP-Nonce with the wpApiSettings.nonce in the headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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